PHP中的排名计算

时间:2016-07-14 08:49:23

标签: php

我想计算学生的等级。等级计算的条件是,如果两个或更多学生具有相同的总分,则他们将获得相同的等级。然后下一个等级将从Previous_rank +重复记录数开始。

这是我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>

    <File name="File" fileName="logs/error.log"
          immediateFlush="true" append="true">
        <PatternLayout pattern=" \n\n %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>

    <RandomAccessFile name="File" fileName="logs/journal.log" immediateFlush="true" append="true">
        <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] %msg%n"/>
    </RandomAccessFile>

    <File name="JournalJSON" fileName="logs/journalJSON.log" immediateFlush="true" append="false">
        <JSONLayout complete="true" charset="UTF-8" compact="false" eventEol="false"/>
    </File>

</appenders>

<Loggers>

    <Logger name="com.ottercoder.controller" level="error" additivity="false">
        <AppenderRef ref="Error" level="error"/>
        <AppenderRef ref="Console" level="error"/>
    </Logger>

    <Logger name="com.ottercoder.service" level="info" additivity="false">
        <appender-ref ref="JournalJSON" level="info"/>
        <appender-ref ref="Journal" level="info"/>
        <appender-ref ref="Console" level="info"/>
    </Logger>

    <Root level="warn">
        <AppenderRef ref="Console"/>
    </Root>
</Loggers>


</configuration>

我的预期输出就像

<?php
$numbers = array( 101, 201, 301, 301, 401, 501, 501);
rsort($numbers);

$arrlength = count($numbers);
$rank = 1;
   $rt=0;
for($x = 0; $x < $arrlength; $x++) {
    if ($x==0) {
         echo $numbers[$x]."- Rank".($rank);
    }

   elseif ($numbers[$x] != $numbers[($x-1)]) {

            echo $numbers[$x]."- Rank".($rank);
            $rt=$rank;
           }
   else{
        echo $numbers[$x]."- Rank".($rt);
            }
      $rank++;
   echo "<br>";
}
?>

4 个答案:

答案 0 :(得分:2)

此代码可以满足您的需求

        <?php
$numbers = array( 101, 201, 301, 301, 401, 401, 401, 501, 501, 501, 501);
rsort($numbers);

$arrlength = count($numbers);
$rank = 1;
$prev_rank = $rank;

for($x = 0; $x < $arrlength; $x++) {

    if ($x==0) {
         echo $numbers[$x]."- Rank".($rank);
    }

   elseif ($numbers[$x] != $numbers[$x-1]) {
        $rank++;
        $prev_rank = $rank;
        echo $numbers[$x]."- Rank".($rank);
   }

   else{
        $rank++;
        echo $numbers[$x]."- Rank".($prev_rank);
    }

   echo "<br>";
}
?>

问题出在$ rt变量上,你设置为0,但应该跟踪之前的排名。

答案 1 :(得分:0)

<?php

    $numbers = array( 101, 201, 301, 301, 401, 501, 501,601,701,701,701,801,901);

    rsort($numbers);

    $arr = $arr1 = array();

    foreach ($numbers as $key => $value) {
        $arr[$value][] = $value;
    }

    $i = $j = 1;

    foreach ($arr as $key => $value) {

        foreach ($value as $key1 => $value1) {
            echo $key."- Rank".$i;
            echo "<br>";
            $j++;
        }
    $i = $j;

    }
?>

答案 2 :(得分:0)

仍然可以减少循环

 <?php

$numbers = array( 101, 201, 301, 301, 401, 501, 501,601,701,701,701,801,901);
rsort($numbers);
$rank=1;
$prev=$numbers[0];
foreach($numbers as $number){
  if($prev!=$number){
    $prev=$number;
    $rank++;
  }
  echo $number."=>Rank -".$rank."\n";
}
?>

相同的输出,但删除了不必要的循环。

答案 3 :(得分:0)

单循环适用于所有情况的PHP代码

rsort($numbers);

$arrlength = count($numbers);
$rank = 1;
$prev_rank = $rank;
for($x = 0; $x < $arrlength; $x++) {
    if ($x==0) {
         echo $numbers[$x]."- Rank".($rank);
    }elseif ($numbers[$x] != $numbers[$x-1]) {
        $rank++;
        $prev_rank = $rank;
        echo $numbers[$x]."- Rank".($rank);
   }else{
        $rank;
        echo $numbers[$x]."- Rank".($prev_rank);
    }
   echo "<br>";
}
?>