题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
#include <stdio.h> int main(){ int number, way; while (scanf("%d %d", &number, &way) != EOF){ //定义一个student结构体,包含学号和成绩 struct student{ char name[10]; int grade; }student[number]; //输入学生信息 for (int i = 0; i < number; ++i) { scanf("%s %d", student[i].name, &student[i].grade); } //冒泡排序 struct student temp; for (int i = 0; i < number-1; ++i) { for (int j = 0; j < number - 1 - i; ++j) { if(way == 1 && (student[j].grade > student[j+1].grade) || way == 0 && (student[j].grade < student[j+1].grade)){ temp = student[j]; student[j] = student[j+1]; student[j+1] = temp; } } } //输出 for (int i = 0; i < number; ++i) { printf("%s %d\n", student[i].name, student[i].grade); } } return 0; }