JhonXu @ 2024-02-21 22:24:07
//P1104 生日
#include<iostream>
#include<algorithm>
#define N 100
using namespace std;
struct student{
string s;
int y;
int m;
int d;
};
bool cmp(student x,student y1){
if(x.y<y1.y)
return 1;
if(x.y==y1.y&&x.m<y1.m)
return 1;
if(x.y==y1.y&&x.m==y1.m&&x.d<y1.d)
return 1;
return 0;
}
int main(){
int n;
student stu[N];
cin >> n;
for(int i=0;i<n;i++){
cin >> stu[i].s >> stu[i].y >> stu[i].m >> stu[i].d;
}
sort(stu,stu+n,cmp);
//(如果有两个同学生日相同,输入靠后的同学先输出)
//这里对已经排序好的数组进行调整,如果相邻的前后相同则把后面的换到前面来
for(int i=0;i<n-1;i++){
if((stu[i].y==stu[i+1].y)&&
(stu[i].m==stu[i+1].m)&&
(stu[i].d==stu[i+1].d)){
student t=stu[i];
stu[i]=stu[i+1];
stu[i+1]=t;
}
}
for(int i=0;i<n;i++)
cout << stu[i].s << endl;
return 0;
}
输入:
70
Zhaojiang 1991 1 8
Yangchu 1993 1 7
Kangyi 1993 2 9
Wangxing 1992 10 20
Liyue 1991 3 14
Qianmu 1993 1 2
Sunxian 1994 5 7
Liyue 1994 10 10
Devon 1988 1 1
Lindsay 1988 1 2
Guning 1989 12 30
Hanmei 1994 1 1
Songting 1992 6 12
Guofan 1994 5 5
Yangyu 1991 9 13
Columbia 1988 2 1
Ainey 1988 2 2
Averill 1989 2 3
Bronson 1990 2 3
Corliss 1991 3 1
Lari 1991 3 2
Ballard 1992 8 20
Brown 1994 1 17
Courtney 1989 10 17
Lark 1990 6 5
Bancroft 1988 1 10
Buck 1988 9 10
Daralis 1992 9 10
Leigh 1993 9 10
Barclay 1988 11 10
Buckley 1989 11 10
Dawn 1990 11 10
Qiujingya 1990 5 8
Luowen 1990 4 6
Heling 1990 12 11
Luopeng 1992 1 9
Pengwei 1993 2 3
Mating 1992 1 18
Chenya 1990 4 5
Tantian 1989 9 2
Leslie 1991 11 10
Barden 1992 11 10
Bud 1993 11 10
Demelza 1994 11 10
Liberty 1989 11 9
Barnett 1988 11 9
Burgess 1992 11 9
Maliang 1990 1 2
Wangxin 1992 12 3
Pangcheng 1990 7 28
Lingshuai 1991 2 9
Xiebing 1991 11 10
Yangyang 1991 3 20
Shejing 1993 9 30
Yuanhui 1992 3 3
Liufang 1993 3 30
Ansley 1993 11 9
Brishen 1994 11 9
Chelsea 1988 7 27
Kirsten 1990 7 27
Atherol 1991 7 27
Brock 1989 7 27
Claiborne 1992 7 27
Kismet 1993 7 24
Atwater 1994 7 24
Broderick 1990 2 14
Clovis 1991 3 13
Kyla 1990 1 19
Twood 1991 1 19
Bromley 1992 1 19
输出:
Devon
Lindsay
Bancroft
Columbia
Ainey
Chelsea
Buck
Barnett
Barclay
Averill
Brock
Tantian
Courtney
Liberty
Buckley
Guning
Maliang
Kyla
Bronson
Broderick
Chenya
Luowen
Qiujingya
Lark
Kirsten
Pangcheng
Dawn
Heling
Zhaojiang
Twood
Lingshuai
Corliss
Lari
Clovis
Liyue
Yangyang
Atherol
Yangyu
Leslie
Xiebing
Luopeng
Mating
Bromley
Yuanhui
Songting
Claiborne
Ballard
Daralis
Wangxing
Burgess
Barden
Wangxin
Qianmu
Yangchu
Pengwei
Kangyi
Liufang
Kismet
Leigh
Shejing
Ansley
Bud
Hanmei
Brown
Guofan
Sunxian
Atwater
Liyue
Brishen
Demelza
by JhonXu @ 2024-02-21 22:45:03
服了,算是警示后人吧。 sort是不稳定排序,改用stable_sort就AC了