题目描述
有N个学生的数据,将学生数据按成绩由低到高排序,如果成绩相同则按姓名首字母序排序,如果首字母也相同则按照年龄排序,并输出N个学生排序后的信息。
输入
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整型数)、成绩(小于等于100的正数)。
3
abc 20 99
bcd 19 97
bed 20 97
输出
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩
bcd 19 97
bed 20 97
abc 20 99
AC代码:
#include <algorithm>
#include <iostream>
using namespace std;
typedef struct student {
char name[100];
int age;
int score;
};
int main() {
int t;
cin >> t;
student *stu = new student[t];
for (int i = 0; i < t; i++) {
cin >> stu[i].name;
cin >> stu[i].age;
cin >> stu[i].score;
}
student temp;
for (int i = 0; i < t; i++) {
for (int j = i; j < t; j++) {
if (stu[i].score > stu[j].score) {
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (int i = 0; i < t; i++) {
for (int j = i; j < t; j++) {
if (stu[i].score == stu[i + 1].score && stu[i].name[0] > stu[i + 1].name[0]) {
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
for (int i = 0; i < t; i++) {
for (int j = i; j < t; j++) {
if (stu[i].score == stu[i + 1].score && stu[i].name[0] == stu[i + 1].name[0]) {
if (stu[i].age > stu[i + 1].age) {
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
}
for (int i = 0; i < t; i++) {
cout << stu[i].name << " ";
cout << stu[i].age << " ";
cout << stu[i].score << endl;
}
delete stu;
return 0;
}