题目要求
代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
//定义表示学生的结构体
struct E {
char name[101];
int age;
int score;
//方法一:运算符重载
bool operator < (const E &b) const {
if (score != b.score) return score < b.score;
int tmp = strcmp(name, b.name);
if (tmp != 0) return tmp < 0;
else return age < b.age;
}
}buf[1000];
//方法二:自定义排序规则
//bool cmp(E a, E b) {
//if (a.score != b.score) {
//return a.score < b.score;
//}
//int tmp = strcmp(a.name, b.name);//比较两个字符串,若s1<s2,返回值<0;若s1=s2,返回值=0;若s1>s2,返回值>0;
//if (tmp != 0)
//return tmp < 0;//若分数相同则名字字典小的排在前面
//else return a.age < b.age;
//}
int main() {
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
scanf("%s %d %d", buf[i].name, &buf[i].age, &buf[i].score);
}
sort(buf, buf+n);
for (int i = 0; i < n; i++)
{
printf("%s %d %d\n", buf[i].name, buf[i].age, buf[i].score);
}
}
return 0;
}