|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
按学生年龄排序。输入n,然后输入n个学生信息(姓名、学号、出生日期年、月、日)按年龄排序(升序)输出。
输入
第一行整数n,第2~n+1行的每一行是一个学生的信息:学号、姓名、出生年月日
输出
已按年龄排序的学生信息,每行一个学生
样例输入
4
1002 John 2001 12 25
1001 Mary 2000 10 1
1003 Mark 2001 3 8
1004 Tony 1999 5 13
样例输出
1002 John 2001.12.25
1003 Mark 2001.3.8
1001 Mary 2000.10.1
1004 Tony 1999.5.13
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct birth
{
int y, m, d;
};
struct stu
{
int num;
string name;
birth birth1;
};
int main()
{
int n;
string name1;
cin >> n;
int y, m, d, number;
stu student;
birth birthday;
vector<stu> stus;
for (int i = 0; i < n; i++)
{
cin >> number >> name1 >> y >> m >> d;
birthday.y = y;
birthday.m = m;
birthday.d = d;
student.num = number;
student.name = name1;
student.birth1 = birthday;
stus.push_back(student);
}
for (int i = 0; i < stus.size(); i++)
{
for (int j = 1; j < stus.size(); j++)
{
if (stus[j - 1].birth1.y < stus[j].birth1.y)
{
stu temp = stus[j - 1];
stus[j - 1] = stus[j];
stus[j] = temp;
}
else if (stus[j - 1].birth1.y == stus[j].birth1.y)
{
if (stus[j - 1].birth1.m < stus[j - 1].birth1.m)
{
stu temp = stus[j - 1];
stus[j - 1] = stus[j];
stus[j] = temp;
}
else if (stus[j - 1].birth1.m == stus[j].birth1.m)
{
if (stus[j - 1].birth1.d < stus[j].birth1.d)
{
stu temp = stus[j - 1];
stus[j - 1] = stus[j];
stus[j] = temp;
}
}
}
}
}
for (int i = 0; i < stus.size(); i++)
{
cout << stus[i].num << " " << stus[i].name << " " << stus[i].birth1.y << "." << stus[i].birth1.m << "." << stus[i].birth1.d << endl;
}
return 0;
}
|
|