TypeCodes

去掉最高分和最低分求平均分数的问题

在百度知道看到个问题,原地址不记得了。题目大致是5位选手参加某歌唱比赛,然后6个评委依次为每位选手打分,最终要求出每位选手在去掉一个最高分和最低分后,自己的平均成绩是多少。然后有人给出了一个比较巧妙的答案,遍历二维数组中的每个元素的时候,“找出”最大值和最小值,然后就去掉这个最大值和最小值求出平均值了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * 歌唱比赛评委评分,去掉最高分和最低分后求出平均分
 * @Filename:  singerscore2.c
 * @Source  :  百度知道
 */

#include <stdio.h>

int main( int argc, char * argv[] )
{
    //所有选手的评分
    float SoreData[5][6] = 
    {
        {9.31, 9.20, 9.00, 9.40, 9.35, 9.20},
        {9.71, 9.52, 9.50, 9.66, 9.49, 9.57},
        {8.89, 8.80, 9.10, 9.25, 8.90, 9.00},
        {9.38, 9.50, 9.40, 9.20, 9.90, 8.90},
        {9.30, 8.84, 9.40, 9.45, 9.10, 8.89}
    };

    float max, min, sum, average;
    int i, j;
    for( i=0; i<sizeof(SoreData)/sizeof(SoreData[0]); i++ )
    {
        //初始化每个选手的总分数, 最低分, 最高分
        sum = 0; min = 10; max = 0;

        for( j=0; j<6; j++)
        {
            if( SoreData[i][j] > max )
                max = SoreData[i][j];
            if( SoreData[i][j] < min )
                min = SoreData[i][j];
            sum += SoreData[i][j];
        }
        average = (sum-min-max)/4;

        //打印出结果
        printf( "No.[%d] total scores are:[%f], average scores are:[%.2f]\n\n", i+1, sum-min-max, (sum-min-max)/4 );
    }

    return 0;
}

测试结果:

No.[1] total scores are:[37.060003], average scores are:[9.27]

No.[2] total scores are:[38.249997], average scores are:[9.56]

No.[3] total scores are:[35.890002], average scores are:[8.97]

No.[4] total scores are:[37.480000], average scores are:[9.37]

No.[5] total scores are:[36.689996], average scores are:[9.17]

去掉最高分和最低分后求出平均分

自己想了想,还可以用冒泡排序法:先将二维数组中的每一行(即“一维数组”)用冒泡排序法排序,然后每一行中的起始元素和最后一个元素都不用管(分别是最小值、最大值),直接求平均值就行了。实现代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * 歌唱比赛评委评分,去掉最高分和最低分后求出平均分
 * @Filename:  singerscore.c
 * @author  :  vfhky 2014.11.10 https://typecodes.com
 */

#include <stdio.h>

//一维字符数组的冒泡排序
void BubbleSort( float array[], int n )
{
    int i=0, j=0; 
    float temp=0;
    int flag = 0;
    for( ; i<n-1; i++ )
    {
        flag = 0;
        for( j=n-1; j>i; j-- )
        {
            if( array[j] < array[j-1] )
            {
                temp = array[j];
                array[j] = array[j-1];
                array[j-1] = temp;
                flag = 1;
            }
        }

        if( flag == 0 )
            break;
    }
}


int main( int argc, char * argv[] )
{
    int i, j;

    //所有选手的评分
    float doubleSoreData[5][6] = 
    {
        {9.31, 9.20, 9.00, 9.40, 9.35, 9.20},
        {9.71, 9.52, 9.50, 9.66, 9.49, 9.57},
        {8.89, 8.80, 9.10, 9.25, 8.90, 9.00},
        {9.38, 9.50, 9.40, 9.20, 9.90, 8.90},
        {9.30, 8.84, 9.40, 9.45, 9.10, 8.89}
    };

    //二维数组冒泡排序
    for( i=0; i<5; i++ )
    {
        BubbleSort( doubleSoreData[i], 6 );
    }

    //初始化一个数组指针
    float (*p)[6];
    p = doubleSoreData;

    //声明每个选手的平均分
    float averagescore;

    //循环读取数据
    for( i=0; i<5; i++ )
    {
        //初始化每一个选手的平均分
        averagescore = 0;

        //除去最高/低位的数据
        for( j=1; j<5; j++ )
        {
            /**
             * 利用数组读出数据
             * printf( "doubleSoreData[%d][%d]=[%f]\n", i, j, doubleSoreData[i][j] );
             * averagescore += doubleSoreData[i][j];
             */

            //利用数组指针读出数据
            printf( "*((p+%d)[%d])=[%.2f]  ", i, j, (*(p+i))[j] );
            averagescore += (*(p+i))[j];
        }

        //打印出结果
        printf( "\nNo.[%d] total scores are:[%f], average scores are:[%.2f]\n\n", i+1, averagescore, averagescore/4 );
    }

    return 0;
}

测试结果:

*((p+0)[1])=[9.20]  *((p+0)[2])=[9.20]  *((p+0)[3])=[9.31]  *((p+0)[4])=[9.35]  
No.[1] total scores are:[37.059998], average scores are:[9.26]

*((p+1)[1])=[9.50]  *((p+1)[2])=[9.52]  *((p+1)[3])=[9.57]  *((p+1)[4])=[9.66]  
No.[2] total scores are:[38.250000], average scores are:[9.56]

*((p+2)[1])=[8.89]  *((p+2)[2])=[8.90]  *((p+2)[3])=[9.00]  *((p+2)[4])=[9.10]  
No.[3] total scores are:[35.889999], average scores are:[8.97]

*((p+3)[1])=[9.20]  *((p+3)[2])=[9.38]  *((p+3)[3])=[9.40]  *((p+3)[4])=[9.50]  
No.[4] total scores are:[37.480000], average scores are:[9.37]

*((p+4)[1])=[8.89]  *((p+4)[2])=[9.10]  *((p+4)[3])=[9.30]  *((p+4)[4])=[9.40]  
No.[5] total scores are:[36.690002], average scores are:[9.17]

二维数组冒泡排序

打赏支持

Comments »