题目描述
​ 输入n个学生的信息,包括姓名、性别、出生年月。

要求按年龄从小到大一次输出这些学生的信息。

数据保证没有学生同年同月出生。

输入
​ 第一行一个整数n,表示学生人数,n≤100。

接下来n行,每一行依次输入学生的姓名、性别、出生年份。出生月份。

输出
​ 按年龄从小到大,一行输出一个学生的原始信息。

样例输入
5
John male 1999 12
David female 1999 8
Jason male 1998 11
Jack female 1998 8
Kitty female 2000 7
样例输出
Kitty female 2000 7
John male 1999 12
David female 1999 8
Jason male 1998 11
Jack female 1998 8
数据规模与约定
​ 时间限制:1 s

内存限制:256 M

100% 的数据保证 1≤n≤100

#include <iostream>
#include <algorithm>
using namespace std;

struct student {
string name;
string sex;
int year;
int month;
};
bool cmp(student a, student b) {
if (a.year == b.year) {
return a.month > b.month;
}
return a.year > b.year;
}
int main() {
student stu[105];
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> stu[i].name >> stu[i].sex >> stu[i].year>> stu[i].month;
sort(stu, stu + n, cmp);
for (int i = 0; i < n; i++)
cout << stu[i].name << " " << stu[i].sex << " " << stu[i].year
<< " " << stu[i].month << endl;
return 0;
}
#include <iostream>
using namespace std;

struct student {
string name;
string sex;
int year;
int month;
};

int main() {
student stu[105];
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> stu[i].name >> stu[i].sex >> stu[i].year>> stu[i].month;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i-1; j++) {
if (stu[j].year < stu[j + 1].year ||
(stu[j].year == stu[j + 1].year && stu[j].month < stu[j + 1].month)) {
swap(stu[j], stu[j + 1]);
}
}
}
for (int i = 0; i < n; i++)
cout << stu[i].name << " " << stu[i].sex << " " << stu[i].year
<< " " << stu[i].month << endl;
return 0;
}
#include <iostream>
using namespace std;

struct student {
string name;
string sex;
int year;
int month;
};

int main() {
student stu[105];
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> stu[i].name >> stu[i].sex >> stu[i].year>> stu[i].month;
for (int i = 0; i < n; i++) {
for (int j = i; j < n ; j++) {
if (stu[i].year < stu[j].year ||
(stu[i].year == stu[j].year && stu[i].month < stu[j].month)) {
swap(stu[i], stu[j]);
}
}
}
for (int i = 0; i < n; i++)
cout << stu[i].name << " " << stu[i].sex << " " << stu[i].year
<< " " << stu[i].month << endl;
return 0;
}