One_last_Spider_man @ 2023-09-02 11:38:28
原代码(#5 WA):
#include <iostream>
#include <algorithm>
using namespace std;
struct sb{
string na;
int n,y,r;
}a[109];
bool cmp(sb a,sb b){
if(a.n!=b.n) return a.n<b.n;
if(a.y!=b.y) return a.y<b.y;
if(a.r!=b.r) return a.r<b.r;
return 1;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i].na>>a[i].n>>a[i].y>>a[i].r;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;++i)
cout<<a[i].na<<endl;
return 0;
}
改后(AC):
#include <iostream>
#include <algorithm>
using namespace std;
struct sb{
string na;
int n,y,r;
}a[109];
bool cmp(sb a,sb b){
if(a.n!=b.n) return a.n<b.n;
if(a.y!=b.y) return a.y<b.y;
if(a.r!=b.r) return a.r<b.r;
return 1;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i].na>>a[i].n>>a[i].y>>a[i].r;
stable_sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;++i)
cout<<a[i].na<<endl;
return 0;
}
啊啊啊啊啊,为啥鸭,想不明白,107题单其他题都用sort都能过,怎么就它不行。
by DAMDAM @ 2023-09-02 11:41:40
(如果有两个同学生日相同,输入靠后的同学先输出)
根据题意,应当使用稳定排序stable_sort
,而不是sort
by One_last_Spider_man @ 2023-09-02 12:22:51
ou,我理解理解