P1104 生日

生日

题目描述

cjf 君想调查学校 OI 组每个同学的生日,并按照年龄从大到小的顺序排序。但 cjf 君最近作业很多,没有时间,所以请你帮她排序。

输入格式

输入共有 n+1 行,

1 行为 OI 组总人数 n

2 行至第 n+1 行分别是每人的姓名 s、出生年 y、月 m、日 d

输出格式

输出共有 n 行,

n 个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)

样例 #1

样例输入 #1

3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1

样例输出 #1

Luowen
Yangchu
Qiujingya

提示

数据保证,1<n<1001|s|<20。保证年月日实际存在,且年份 [1960,2020]

2. 题解

2.1

思路

这里必须要注意,sort排序不能保证排序后集合的相对顺序稳定性,也就是说我们不凭借标记无法判断谁先输入谁后输入
所以这里加入一个id用来标记输入顺序。

代码

#include<bits/stdc++.h>
using namespace std;
struct Student{
	string name;
	int year;
	int month;
	int day;
	int id;
};

bool cmp(Student stu1, Student stu2){
	if(stu1.year != stu2.year) return stu1.year < stu2.year;
	else if(stu1.month != stu2.month) return stu1.month < stu2.month;
	else if(stu1.day != stu2.day) return stu1.day < stu2.day;
	else return stu1.id > stu2.id;
}

int main(){
	int n;
	cin >> n;
	vector<Student> stu(n);
	for(int i = 0; i < n; i++){
		cin >> stu[i].name >> stu[i].year >> stu[i].month >> stu[i].day;
		stu[i].id = i + 1;
	}
	sort(stu.begin(), stu.end(), cmp);
	for(int i = 0; i < n; i++){
		cout << stu[i].name << endl;
	}
}
posted @   DawnTraveler  阅读(106)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示