统计成绩:给出n个学生的考试成绩表,每条信息由姓名和分数组成,试设计一个算法,按名次列出每个学生的姓名与分数c语言代码
时间: 2024-01-02 22:04:46 浏览: 73
以下是用C语言实现的统计成绩算法:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
char name[20];
int score;
};
void bubbleSort(struct Student students[], int n);
void printRank(struct Student students[], int n);
int main() {
struct Student students[MAX_STUDENTS];
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
// 输入学生姓名和分数
for (int i = 0; i < n; i++) {
printf("Enter the name and score of student %d: ", i + 1);
scanf("%s%d", students[i].name, &students[i].score);
}
// 按照分数从高到低排序
bubbleSort(students, n);
// 输出排名
printRank(students, n);
return 0;
}
void bubbleSort(struct Student students[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].score < students[j + 1].score) {
// 交换位置
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void printRank(struct Student students[], int n) {
printf("\nRank\tName\tScore\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%d\n", i + 1, students[i].name, students[i].score);
}
}
```
该算法首先输入学生姓名和分数,然后使用冒泡排序按照分数从高到低排序,最后输出排名。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)