0

Please help me in ranking with a formula like for example:

std_no    score        rank  

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

the above example is correct but i want this like.

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

how i get 3.5? formula rank. 2,3,4,5 add then divided 4 which is the quantity of same rank 45.

4

1 回答 1

0

因为没有指定标签或语言,所以我选择 perl(转换为 php:http ://www.cs.wcupa.edu/~rkline/perl2php/ ):

    #!/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);

我希望它会帮助你。

警告 !!此代码未优化!

于 2013-08-19T11:19:24.947 回答