Sort by birthday

Title description:

The School of Garlic Head has started. The teacher will count the birthdays of everyone in the class and sort them according to the date of birth from morning to night.

Input format:

The first line is an integer n (1≤n≤100)n (1≤n≤100), the number of class members.

Next nn lines, each line contains a string ss and three integers y, m, dy, m, d, indicating that the date of birth of the student whose name is ss is mm, month, dd, yy.

Ensure that all dates are legal and that the name consists of lowercase letters and does not exceed 2020 characters.

Output format:

Output nn lines, each line represents a name. If two classmates have the same birth date, the classmate with the later input will be output first.

Sample input:

``````3
qwb 1996 6 30
gyt 1995 7 28
wc  1996 6 30
``````

Sample output:

``````gyt
wc
qwb
``````

sort:
``````#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Node {

string name;	//名字
int in;			//输入顺序
int y, m, d;	//年月日
};
bool cmp(Node a, Node b) {

if (a.y == b.y) {

if (a.m == b.m) {

if (a.d == b.d)
return a.in > b.in;
return a.d < b.d;
}
return a.m < b.m;
}
return a.y < b.y;
//比较规则，这里自己好好思考一下，年小排在前，相等看月，再相等看日，再相同看输入顺序
}
int main() {

int n; Node a;
cin >> n;
vector<Node> v;
for (int i = 1; i <= n; ++i)
cin >> a.name >> a.y >> a.m >> a.d, a.in = i, v.push_back(a);
sort(v.begin(), v.end(), cmp);
for (int i = 0; i < n; ++i)
cout << v[i].name << endl;
return 0;
}
``````
priority_queue:
``````#include<iostream>
#include<queue>
using namespace std;
struct Node {

string name;	//名字
int in;			//输入顺序
int y, m, d;	//年月日
};
struct cmp {

bool operator()(Node a, Node b){

if (a.y == b.y) {

if (a.m == b.m) {

if (a.d == b.d)
return a.in < b.in;
return a.d > b.d;
}
return a.m > b.m;
}
return a.y > b.y;
}//比较规则，这里自己好好思考一下，年小排在前，相等看月，再相等看日，再相同看输入顺序
};
int main() {

int n; Node a;
cin >> n;
priority_queue<Node, vector<Node>, cmp> q;
for (int i = 1; i <= n; ++i)
cin >> a.name >> a.y >> a.m >> a.d, a.in = i, q.push(a);
for (int i = 0; i < n; ++i)
cout << q.top().name << endl, q.pop();
return 0;
}
``````

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/113443202
Recommended
Ranking
Daily