duaneryan @ 2024-10-28 20:51:34
在生日相同时,怎么让先输入的同学先输出? 求大佬救我一命!!!
#include<bits/stdc++.h>
using namespace std;
int n;
struct ren{
string m;
int n,yu,r;
} s[105];
bool cmp(ren x,ren y){
if(x.n==y.n){
if(x.yu==y.yu){
return x.r<y.r;
}
return x.yu<y.yu;
}
return x.n<y.n;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i].m>>s[i].n>>s[i].yu>>s[i].r;
}
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<s[i].m<<endl;
}
return 0;
}
谢谢啦!
by lty2023 @ 2024-10-28 20:58:08
结构体加个num变量,代表第几个人 @duaneryan
by a_study_xxs @ 2024-10-28 20:58:55
我教你:你可以在结构体里存它是第几个输入的,结构体要改一下:
struct ren{
string m;
int n,yu,r,id//id是这个人是第几个输入的;
} s[105];
然后cmp函数也要改一下:
bool cmp(ren x,ren y){
int left=x.n*1000+x.yu*10+x.r;
int right=y.n*1000+y.yu*10+y.r;
if(left!=right)return left<right;
else return x.id<y.id;
}
即可
by a_study_xxs @ 2024-10-28 20:59:38
求关
by a_study_xxs @ 2024-10-28 21:03:59
写反了
应该是:
bool cmp(ren x,ren y){
int left=x.n*1000+x.yu*10+x.r;
int right=y.n*1000+y.yu*10+y.r;
if(left!=right)return left<right;
else return x.id>y.id;
}
by Ff472130 @ 2024-10-28 21:05:23
给结构体加一个id数值,然后在输入的时候记录,在cmp函数里比较就行了
代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct ren{
string m;
int n,yu,r,id;
} s[105];
bool cmp(ren x,ren y){
if(x.n==y.n){
if(x.yu==y.yu){
if(x.r==y.r){
return x.id>y.id;
}
return x.r<y.r;
}
return x.yu<y.yu;
}
return x.n<y.n;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i].m>>s[i].n>>s[i].yu>>s[i].r;
s[i].id=i;
}
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<s[i].m<<endl;
}
return 0;
}
求关
by a_study_xxs @ 2024-10-28 21:06:25
#include<bits/stdc++.h>
using namespace std;
int n;
struct ren{
string m;
int n,yu,r,id;
} s[105];
bool cmp(ren x,ren y){
int left=x.n*10000+x.yu*100+x.r;
int right=y.n*10000+y.yu*100+y.r;
if(left!=right)return left<right;
else return x.id>y.id;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i].m>>s[i].n>>s[i].yu>>s[i].r;
s[i].id = i ;
}
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;i++){
cout<<s[i].m<<endl;
}
return 0;
}
求关
by a_study_xxs @ 2024-10-28 21:07:02
@Ff472130 @lty2023 @mzh98K 三帮一
by duaneryan @ 2024-10-29 12:14:07
没想到那么多人帮忙啊,真的谢谢了! @a_study_xxs @Ff472130 @lty2023