题目描述
有N个学生的数据,将学生数据按成绩从低到高排序,如果成绩相同则按姓名字符的字典序由小到大排序,如果姓名的字典序也相同则按照学生的年龄从小到大排序,并输出N个学生排序后的信息。
输入描述
测试数据有多组,每组输入第一行有一个整数N(N<=1000),接下来的N行包括N个学生的数据。
每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。
输出描述
将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。
然后输出学生信息,按照如下格式:
姓名 年龄 成绩
学生姓名的字母序区分字母的大小写,如A要比a的字母序靠前(因为A的ASC码比a的ASC码要小)。
示例
输入
3
abc 20 99
bcd 19 97
bed 20 97
输出
bcd 19 97
bed 20 97
abc 20 99
Code
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
class student {
public:
string name;
int age;
int score;
};
student stu[1000];
bool comp(student a, student b)
{
if (a.score != b.score)
return a.score < b.score;//递增
else if (a.name!= b.name)
return a.name< b.name;
else
return a.age < b.age;
}
int main()
{
int n;
while (cin >> n)
{
getchar();
for (int i = 0; i < n; i++)
{
cin >> stu[i].name >> stu[i].age >> stu[i].score;
}
sort(stu, stu + n, comp);
for (int i = 0; i < n; i++)
{
cout << stu[i].name << " " << stu[i].age << " " << stu[i].score << endl;
}
}
return 0;
}