c语音成绩排名5人三成绩代码
时间: 2024-02-01 14:13:47 浏览: 18
以下是一个简单的 C 语言程序,可以实现对五个人的三个成绩进行排名,并输出排名结果和总分最高的人的信息。
```c
#include <stdio.h>
struct student {
char name[20];
int score[3];
int total;
};
int main() {
struct student stu[5];
int i, j, sum, max, index;
// 输入学生信息和成绩
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的姓名和成绩:\n", i + 1);
scanf("%s %d %d %d", stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]);
sum = 0;
for (j = 0; j < 3; j++) {
sum += stu[i].score[j];
}
stu[i].total = sum;
}
// 排序
for (i = 0; i < 5; i++) {
max = stu[i].total;
index = i;
for (j = i + 1; j < 5; j++) {
if (stu[j].total > max) {
max = stu[j].total;
index = j;
}
}
if (index != i) {
struct student tmp = stu[i];
stu[i] = stu[index];
stu[index] = tmp;
}
}
// 输出排名结果和总分最高的人的信息
printf("排名结果如下:\n");
for (i = 0; i < 5; i++) {
printf("第%d名:%s,总分:%d\n", i + 1, stu[i].name, stu[i].total);
}
printf("总分最高的人是:%s,总分:%d\n", stu[0].name, stu[0].total);
return 0;
}
```
程序中使用了一个结构体 `student` 来存储每个学生的姓名、三个成绩和总分。首先通过循环输入五个学生的信息和成绩,然后计算每个学生的总分,并将其保存在结构体中。接着使用冒泡排序对五个学生进行排名,最后输出排名结果和总分最高的人的信息。
注意:本程序中未进行输入验证,实际应用时需要增加相应的输入验证代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)