学生排名得分的php代码

学生排名得分的php代码,php,ranking,Php,Ranking,请用一个公式帮助我排名,例如: std_no score rank 1 50 1 4 45 2 3 45 3 2 45 4 5 45 5 6 30 6 上面的例子是正确的,但我希望这样 std_no score rank 1

请用一个公式帮助我排名,例如:

std_no    score        rank  

 1        50           1
 4        45           2
 3        45           3
 2        45           4
 5        45           5
 6        30           6
上面的例子是正确的,但我希望这样

std_no   score        rank  

 1       50            1
 4       45            3.5
 3       45            3.5
 2       45            3.5
 5       45            3.5
 6       30            6

我怎么得到3.5分?公式等级。2,3,4,5相加,然后除以4,这是相同秩45的数量。

因为没有指定标记或语言,所以我选择perl(要转换为php:):

#/usr/bin/perl
严格使用;
使用警告;
使用数据::转储程序;
使用克隆qw(克隆);
亚米兰克
{
我的($情侣,$tmpInput)=@;
我的$minRank=@$couple[1];
foreach my$key(key%{$tmpInput}){
我的$StudentCouple=$tmpInput->{$key};
如果(@$StudentCouple[0]==@$couple[0]&&&&@$StudentCouple[1]<$minRank){
$minRank=@$StudentCouple[1];
}
}
返回$minRank;
}
次最大秩
{
我的($情侣,$tmpInput)=@;
我的$maxRank=@$couple[1];
foreach my$key(key%{$tmpInput}){
我的$StudentCouple=$tmpInput->{$key};
if(@$StudentCouple[0]==@$couple[0]&&&&$$StudentCouple[1]>$maxRank){
$maxRank=@$StudentCouple[1];
}
}
返回$maxRank;
}
次迭代
{
我的($情侣,$tmpInput)=@;
我的$nbIteration=0;
foreach my$key(key%{$tmpInput}){
我的$StudentCouple=$tmpInput->{$key};
如果(@$StudentCouple[0]==@$couple[0]){
$nb++;
}
}
返回$nb;
}
附属电脑银行
{
我的($情侣,$tmpInput)=@;
my$tmpInput1=$tmpInput;
我的$nbIteration=\u nbIteration($couple,$tmpInput1);
我的$rank=@$POWER[1];
如果($nbIteration>1){
my$tmpInput2=$tmpInput;
我的$minRank=\u minRank($couple,$tmpInput2);
my$tmpInput3=$tmpInput;
我的$maxRank=\u maxRank($couple,$tmpInput3);
$rank=($minRank+$maxRank)/2;
}
返回$rank;
}
#标准号=>[分数,排名]
我的%input=(1=>[50,1],
4 => [45, 2], 
3 => [45, 3], 
2 => [45, 4],
5 => [45, 5], 
6 => [30, 6] );
我的%output=%{clone(\%input)};
foreach my$键(键%input){
my$StudentCouple=$input{$key};
我的%tmpInput=%input;
my$outputCourt=$output{$key};
@$outputCourse[1]=\u计算机银行($studentCourse,\%tmpInput);
}
打印转储程序(\%输出);
我希望它能帮助你


警告!!此代码未优化

至少显示你的代码。通常,4名成绩相同的学生都应该是第二名,而不是第3.5名。std_no不应该是唯一的?因为theer是2 std_no 1,但公式是将所有相同分数的排名相加,然后除以相同分数的数量score@binogure对不起,打错了
    #!/usr/bin/perl

    use strict;
    use warnings;
    use Data::Dumper;
    use Clone qw(clone);

    sub _minRank
    {
        my ($couple, $tmpInput)    = @_;
        my $minRank                = @$couple[1];

        foreach my $key (keys %{$tmpInput}) {
            my $StudentCouple = $tmpInput->{$key};

            if (@$StudentCouple[0] == @$couple[0] && @$StudentCouple[1] < $minRank) {
                $minRank = @$StudentCouple[1];
            }
        }
        return $minRank;
    }

    sub _maxRank
    {
        my ($couple, $tmpInput)    = @_;
        my $maxRank                = @$couple[1];

        foreach my $key (keys %{$tmpInput}) {
            my $StudentCouple = $tmpInput->{$key};

            if (@$StudentCouple[0] == @$couple[0] && @$StudentCouple[1] > $maxRank) {
                $maxRank = @$StudentCouple[1];
            }
        }
        return $maxRank;
    }

    sub _nbIteration
    {
        my ($couple, $tmpInput)    = @_;
        my $nbIteration            = 0;

        foreach my $key (keys %{$tmpInput}) {
            my $StudentCouple = $tmpInput->{$key};

            if (@$StudentCouple[0] == @$couple[0]) {
                $nbIteration++;
            }
        }

        return $nbIteration;
    }

    sub _computeRank
    {
        my ($couple, $tmpInput) = @_;

        my $tmpInput1   = $tmpInput;
        my $nbIteration = _nbIteration($couple, $tmpInput1);
        my $rank        = @$couple[1];

        if ($nbIteration > 1) {
            my $tmpInput2   = $tmpInput;
            my $minRank     = _minRank($couple, $tmpInput2);

            my $tmpInput3   = $tmpInput;
            my $maxRank     = _maxRank($couple, $tmpInput3);

            $rank = ($minRank + $maxRank) / 2;
        }

        return $rank;
    }

    #         Std_no => [score, rank]
    my %input = (  1 => [50, 1], 
                   4 => [45, 2], 
                   3 => [45, 3], 
                   2 => [45, 4],
                   5 => [45, 5], 
                   6 => [30, 6] );

    my %output = %{clone(\%input)};

    foreach my $key (keys %input) {
        my $StudentCouple  = $input{$key};
        my %tmpInput       = %input;
        my $outputCouple   = $output{$key};

        @$outputCouple[1] = _computeRank($StudentCouple, \%tmpInput);
    }

    print Dumper(\%output);