c语音成绩排名5人三成绩代码
时间: 2024-02-01 12:13:44 浏览: 16
这是一个简单的C语言程序,可以输入5个学生的成绩并按照成绩从高到低排名,同时输出每个学生的三门课程成绩。
```c
#include <stdio.h>
struct student {
char name[20];
int score[3];
int total;
};
int main() {
struct student stu[5];
int i, j, k;
// 输入每个学生的姓名和成绩
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的姓名:", i + 1);
scanf("%s", stu[i].name);
printf("请输入该学生的三门课程成绩:");
for (j = 0; j < 3; j++) {
scanf("%d", &stu[i].score[j]);
stu[i].total += stu[i].score[j];
}
}
// 按照总分从高到低排序并输出排名和成绩
for (i = 0; i < 4; i++) {
k = i;
for (j = i + 1; j < 5; j++) {
if (stu[j].total > stu[k].total) {
k = j;
}
}
if (k != i) {
struct student temp = stu[i];
stu[i] = stu[k];
stu[k] = temp;
}
}
printf("\n排名\t姓名\t成绩1\t成绩2\t成绩3\t总分\n");
for (i = 0; i < 5; i++) {
printf("%d\t%s\t%d\t%d\t%d\t%d\n", i + 1, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2], stu[i].total);
}
return 0;
}
```
程序中使用了结构体来存储每个学生的姓名和成绩信息,其中`score`数组存储了三门课程的成绩,`total`变量存储了总分。首先通过循环输入每个学生的姓名和成绩,然后再使用选择排序算法将学生按照总分从高到低排序。最后按照排名输出每个学生的姓名和三门课程成绩以及总分。
相关推荐
![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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)