P1104求助,#5#6WA,玄关

P1104 生日

I2147483647I @ 2024-10-27 14:29:10

这是我的老代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=110;
struct oier{
    ll y,m,d,num;
    string name;
}; 
bool cmp(oier x,oier z){
    return x.y<z.y || (x.y==z.y && (x.m<z.m || (x.m==z.m && (x.d<z.d || (x.d==z.d && x.num<z.num))))); 
}
oier a[N];
int main(){
    ll n;
    cin>>n;
    ll s;
    for(s=1;s<=n;s++){
        cin>>a[s].name>>a[s].y>>a[s].m>>a[s].d;
    }
    sort(a+1,a+n+1,cmp);
    for(s=1;s<=n;s++){
        cout<<a[s].name<<endl;
    }
    return 0;
}

这是我自己修改过的:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=110;
struct oier{
    ll y,m,d,num;
    string name;
}; 
bool cmp(oier x,oier z){
    return x.y<z.y || (x.y==z.y && (x.m<z.m || (x.m==z.m && (x.d<z.d || (x.d==z.d && x.num<z.num))))); 
}
oier a[N];
int main(){
    ll n;
    cin>>n;
    ll s;
    for(s=1;s<=n;s++){
        cin>>a[s].name>>a[s].y>>a[s].m>>a[s].d;
        a[s].num=s;//此处
    }
    sort(a+1,a+n+1,cmp);
    for(s=1;s<=n;s++){
        cout<<a[s].name<<endl;
    }
    return 0;
}

然鹅,我发现老代码可以AC#5,修改过的却不行! 求助QwQ,玄关!


by I2147483647I @ 2024-10-27 14:30:23

注:s是循环变量。我就是有这个破习惯


by Zhall_Dreamy @ 2024-10-27 14:44:03

@I2147483647I

注意,题目中说如果有两个同学生日相同,输入靠后的同学先输出

所以:

bool cmp(oier x,oier z){
    return x.y<z.y || (x.y==z.y && (x.m<z.m || (x.m==z.m && (x.d<z.d || (x.d==z.d && x.num<z.num))))); 
}

应该改为

bool cmp(oier x,oier z){
    return x.y<z.y || (x.y==z.y && (x.m<z.m || (x.m==z.m && (x.d<z.d || (x.d==z.d && x.num>z.num))))); 
}

这样就可以了


by Zhall_Dreamy @ 2024-10-27 14:44:31

@I2147483647I 求壶关


by I2147483647I @ 2024-10-29 21:42:15

@Zhall_Dreamy 关注了,谢谢!

此帖结。


|