去掉最高分和最低分求平均分数的问题
在百度知道看到个问题,原地址不记得了。题目大致是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 |
|
测试结果:
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 |
|
测试结果:
*((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 »