结构体应用-—学生成绩排序【问题描述】对某班学生成绩排序。从键盘依次输入6个学生的姓名和成绩并保存然后分别按学生成绩由高到低顺序输出学生姓名和成绩。【输入形式】aaa 50bbb 70ccc 65ddd 90eee 60fff 100【输出形式】fff#100ddd#90bbb#70ccc#65eee#60aaa#50注意:其中#号代表空格用c语言完成
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int score;
};
int main() {
struct Student students[6];
// 输入学生姓名和成绩
for (int i = 0; i < 6; i++) {
scanf("%s %d", students[i].name, &students[i].score);
}
// 冒泡排序,按学生成绩由高到低排序
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6 - 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;
}
}
}
// 输出学生姓名和成绩
for (int i = 0; i < 6; i++) {
printf("%s#%d\n", students[i].name, students[i].score);
}
return 0;
}
输入:
aaa 50
bbb 70
ccc 65
ddd 90
eee 60
fff 100
输出:
fff#100
ddd#90
bbb#70
ccc#65
eee#60
aaa#50
``
原文地址: https://www.cveoy.top/t/topic/hOAB 著作权归作者所有。请勿转载和采集!