编写一个C语言程序,输入n个学生的学号和成绩(有1位小数),按成绩从高到低排序输出,n 小于50,学号小于10位。(要求输出代码包含主函数部分)
时间: 2024-02-05 17:02:32 浏览: 66
以下是一个示例程序,使用冒泡排序对成绩进行从高到低的排序:
```c
#include <stdio.h>
#define MAX_N 50
struct Student {
char id[11];
float score;
};
void sort_students(struct Student students[], int n);
int main() {
int n;
struct Student students[MAX_N];
printf("Enter the number of students (n <= 50): ");
scanf("%d", &n);
printf("Enter the student ID and score for each student:\n");
for (int i = 0; i < n; i++) {
scanf("%s %f", students[i].id, &students[i].score);
}
sort_students(students, n);
printf("Sorted by score:\n");
for (int i = 0; i < n; i++) {
printf("%s %.1f\n", students[i].id, students[i].score);
}
return 0;
}
void sort_students(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;
}
}
}
}
```
程序首先读入学生人数和每个学生的学号和成绩,然后调用 `sort_students()` 函数对学生按成绩排序,最后输出排好序的学号和成绩。
阅读全文