本題模擬歌唱比賽評分:7 位評委打分,去除一個最高分與一個最低分,計算剩餘 5 分平均,保留小數點後兩位。輸入多組測資,每組 7 個分數 + 選手姓名,至 EOF 結束。
%0.2f)。while(scanf("%f", &score[0]) != EOF) 讀取第一個分數作為迴圈條件。sort() 氣泡排序由小到大。printf("%s %0.2f\n", name, avg)。#include <stdio.h>
void sort(float score[]);
int main()
{
int i;
char name[30];
float score[7], sum, avg;
while(scanf("%f", &score[0]) != EOF)
{
sum = 0.0;
for(i = 1; i < 7; i++) scanf("%f", &score[i]);
scanf("%s", name);
sort(score);
for(i = 1; i < 6; i++) sum += score[i];
avg = sum / 5;
printf("%s %0.2f\n", name, avg);
}
return 0;
}
void sort(float score[])
{
int i, j;
float t;
for(i = 0; i < 7; i++)
{
for(j = i; j < 7; j++)
{
if(score[i] > score[j])
{
t = score[i];
score[i] = score[j];
score[j] = t;
}
}
}
}

輸入:10 10 10 10 10 10 9 xiaoyuanwang
排序後:9 10 10 10 10 10 10
去極值:10 10 10 10 10 平均 = 10.00 ✓
輸入:0 0 0 0 0 0 0 beast 平均 = 0.00 ✓
qsort 替代自寫排序。%s 足夠。float 足夠,若要求高精度可用 double。