编写一个对n个数据从大到小的排序C函数,再编写一个计算最后得分的C函数,计算方法是:去除一个最高分,去除一个最低分,其余的平均分为参赛选手的最后得分。并在主函数中调用它们对有n个评委评分,m个选手参赛的最后得分,从大到小排序输出。
时间: 2025-06-15 18:21:53 浏览: 12
为了编写这个程序,我们可以使用C语言的标准库`<stdio.h>`、`<stdlib.h>`和`<time.h>`。首先,我们需要一个函数`sort_scores`来对分数数组降序排列,然后一个函数`calculate_average_score`来计算最后得分,并处理最高分和最低分。最后,在`main`函数中调用这两个函数。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 对数组进行降序排序
void sort_scores(int scores[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (scores[j] < scores[j + 1]) {
int temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
}
}
}
}
// 计算并返回去掉最高分和最低分后的平均分
double calculate_average_score(int scores[], int n) {
// 如果只有一个评委,直接返回该分数
if (n == 1) return scores[0];
int highest = scores[0], lowest = scores[0];
double total = scores[0];
for (int i = 1; i < n - 2; i++) {
if (scores[i] > highest) highest = scores[i];
if (scores[i] < lowest) lowest = scores[i];
total += scores[i];
}
total -= highest - lowest; // 去掉最高分和最低分
return total / (n - 2);
}
// 主函数
int main() {
srand(time(0)); // 设置随机种子,模拟评委评分
int num_judges = 5, num_participants = 4; // 示例值
int scores[num_judges][num_participants]; // 假设每个评委给每个选手打分
// 填充随机分数示例
for (int judge = 0; judge < num_judges; judge++) {
for (int participant = 0; participant < num_participants; participant++) {
scores[judge][participant] = rand() % 100 + 1; // 生成1~100之间的随机数
}
}
printf("原始评委评分:\n");
for (int i = 0; i < num_judges; i++) {
for (int j = 0; j < num_participants; j++) {
printf("%d ", scores[i][j]);
}
printf("\n");
}
// 对评委评分按选手排序
sort_scores(scores[0], num_participants);
double final_scores[num_participants]; // 存储每位选手的最终得分
for (int i = 0; i < num_participants; i++) {
final_scores[i] = calculate_average_score(&scores[0][i], num_judges);
}
// 按得分降序排列选手
sort_scores(final_scores, num_participants);
printf("选手最终得分(从高到低):\n");
for (int i = 0; i < num_participants; i++) {
printf("%d. %.2f\n", i + 1, final_scores[i]);
}
return 0;
}
```
阅读全文
相关推荐



















