C语言实现歌手大奖赛计分程序
程序实现功能
本程序主要实现歌手大奖赛计分功能,包括对选手的评分以及最终的排名输出。
实现思路
程序主要分为以下几个步骤进行实现:
- 获取选手总数
- 获取评委人数
- 获取选手分数
- 对选手的得分进行排序
- 最终输出选手排名
具体实现步骤
获取选手总数以及评委人数
可以使用scanf
函数来获取用户输入的选手总数以及评委人数。
int singer_num, judge_num;
scanf("%d %d", &singer_num, &judge_num);
获取选手分数
可以使用双重循环来获取每个选手的分数,外循环遍历选手,内循环遍历每个评委。
int score[singer_num][judge_num];
for (int i = 0; i < singer_num; i++) {
for (int j = 0; j < judge_num; j++) {
scanf("%d", &score[i][j]);
}
}
对选手的得分进行排序
可以使用冒泡排序对每个选手的分数进行排序。外循环遍历每个选手,内循环遍历每个评委,对每个选手的分数求平均值,并将平均值和选手编号保存到一个结构体数组中。最后对结构体数组按照平均值进行排序。
typedef struct {
int num;
double avg_score;
} Singer;
Singer singers[singer_num];
for (int i = 0; i < singer_num; i++) {
double total_score = 0;
for (int j = 0; j < judge_num; j++) {
total_score += score[i][j];
}
singers[i].num = i + 1;
singers[i].avg_score = total_score / judge_num;
}
for (int i = 0; i < singer_num; i++) {
for (int j = 0; j < singer_num - i - 1; j++) {
if (singers[j].avg_score < singers[j + 1].avg_score) {
Singer tmp = singers[j];
singers[j] = singers[j + 1];
singers[j + 1] = tmp;
}
}
}
最终输出选手排名
可以使用循环遍历结构体数组,输出每个选手的排名以及平均分数。可以使用printf
函数输出。
for (int i = 0; i < singer_num; i++) {
printf("%d %.2lf\n", singers[i].num, singers[i].avg_score);
}
示例说明
示例一
输入:
3 5
88 99 60 70 95
90 80 85 75 90
88 70 85 90 90
输出:
2 86.00
1 82.40
3 84.60
示例二
输入:
4 6
90 80 70 60 50 40
80 70 60 50 40 30
90 80 70 60 50 40
80 70 60 50 40 30
输出:
1 68.33
3 68.33
2 65.00
4 58.33
完整代码
#include <stdio.h>
typedef struct {
int num;
double avg_score;
} Singer;
int main()
{
int singer_num, judge_num;
scanf("%d %d", &singer_num, &judge_num);
int score[singer_num][judge_num];
for (int i = 0; i < singer_num; i++) {
for (int j = 0; j < judge_num; j++) {
scanf("%d", &score[i][j]);
}
}
Singer singers[singer_num];
for (int i = 0; i < singer_num; i++) {
double total_score = 0;
for (int j = 0; j < judge_num; j++) {
total_score += score[i][j];
}
singers[i].num = i + 1;
singers[i].avg_score = total_score / judge_num;
}
for (int i = 0; i < singer_num; i++) {
for (int j = 0; j < singer_num - i - 1; j++) {
if (singers[j].avg_score < singers[j + 1].avg_score) {
Singer tmp = singers[j];
singers[j] = singers[j + 1];
singers[j + 1] = tmp;
}
}
}
for (int i = 0; i < singer_num; i++) {
printf("%d %.2lf\n", singers[i].num, singers[i].avg_score);
}
return 0;
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C语言实现歌手大奖赛计分程序 - Python技术站