c语言习题:唱歌比赛有n名选手,m名评委。 编程实现: (1)评分标准为:去掉两个最高分和两个最低分后取平均分。 (2)每位选手演唱完毕后,现场显示每位评委的打分, 和选手的最后得分。 (3)比赛结束后,从高分到低分,显示所有选手的成绩
时间: 2024-05-16 18:14:29 浏览: 105
以下是C语言的代码实现,实现了上述要求:
```
#include <stdio.h>
#include <stdlib.h>
// 定义常量
#define MAX_SCORE 100 // 最高分数
#define MIN_SCORE 0 // 最低分数
#define MAX_JUDGE 10 // 最多评委人数
#define MIN_JUDGE 1 // 最少评委人数
#define MAX_PLAYER 1000 // 最多选手人数
#define MIN_PLAYER 1 // 最少选手人数
// 计算分数
float calculate_score(int scores[], int n) {
int max1, max2, min1, min2;
float sum = 0.0;
// 找到最高分和最低分
max1 = max2 = MIN_SCORE;
min1 = min2 = MAX_SCORE;
for (int i = 0; i < n; i++) {
if (scores[i] > max1) {
max2 = max1;
max1 = scores[i];
} else if (scores[i] > max2) {
max2 = scores[i];
}
if (scores[i] < min1) {
min2 = min1;
min1 = scores[i];
} else if (scores[i] < min2) {
min2 = scores[i];
}
sum += scores[i];
}
// 去掉最高分和最低分后取平均分
return (sum - max1 - max2 - min1 - min2) / (n - 4);
}
int main() {
int n, m, scores[MAX_JUDGE];
float player_scores[MAX_PLAYER];
float temp_score;
char c;
// 输入选手人数和评委人数
printf("请输入选手人数和评委人数(用空格分隔):");
scanf("%d %d", &n, &m);
if (n < MIN_PLAYER || n > MAX_PLAYER || m < MIN_JUDGE || m > MAX_JUDGE) {
printf("输入错误!\n");
return 1;
}
// 输入每位选手的分数
for (int i = 0; i < n; i++) {
printf("请输入第%d位选手的分数:\n", i + 1);
printf("评委1:");
scanf("%d", &scores[0]);
printf("评委2:");
scanf("%d", &scores[1]);
printf("评委3:");
scanf("%d", &scores[2]);
printf("评委4:");
scanf("%d", &scores[3]);
printf("评委5:");
scanf("%d", &scores[4]);
printf("评委6:");
scanf("%d", &scores[5]);
printf("评委7:");
scanf("%d", &scores[6]);
printf("评委8:");
scanf("%d", &scores[7]);
printf("评委9:");
scanf("%d", &scores[8]);
printf("评委10:");
scanf("%d", &scores[9]);
// 计算该选手最终得分
temp_score = calculate_score(scores, m);
player_scores[i] = temp_score;
// 输出每位评委的分数和该选手的最终得分
printf("选手%d的得分:%.2f\n", i + 1, temp_score);
printf("评委分数:");
for (int j = 0; j < m; j++) {
printf("%d ", scores[j]);
}
printf("\n");
}
// 按照得分从高到低排序并输出所有选手的成绩
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (player_scores[i] < player_scores[j]) {
temp_score = player_scores[i];
player_scores[i] = player_scores[j];
player_scores[j] = temp_score;
}
}
}
printf("所有选手的成绩:\n");
for (int i = 0; i < n; i++) {
printf("%d号选手:%.2f分\n", i + 1, player_scores[i]);
}
return 0;
}
```
阅读全文