c/c++课的作业合集,都是很简单的文件。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

#include <stdio.h>
#include <cstring>
struct Student {
char name[16];
int score;
};
void sort(Student *students, int count);
void copy(Student *target, Student *source);
int main() {
int studentNumber = 0;
scanf("%d", &studentNumber);
Student students[studentNumber];
for (int i = 0; i < studentNumber; ++i) {
scanf("%s %d", students[i].name, &students[i].score);
}
sort(students, studentNumber);
for (Student student : students) {
printf("%s %d\n", student.name, student.score);
}
}
void sort(Student *students, int count) {
Student temp{};
for (int i = 0; i < count - 1; ++i) {
for (int j = 0; j < count - 1 - i; ++j) {
if (students[j].score < students[j + 1].score) {
copy(&temp, &students[j + 1]);
copy(&students[j + 1], &students[j]);
copy(&students[j], &temp);
}
}
}
}
void copy(Student *target, Student *source) {
strcpy(target->name, source->name);
target->score = source->score;
}