两个方式 一种80 一种64 求助!!!

P1104 生日

danaqi_ @ 2024-07-12 09:03:14

rt
用的sort 80

#include<bits/stdc++.h>
using namespace std;
struct Node{
    char s[25];
    int y,m,d,id;
};
Node a[105];
bool cmp(Node x,Node y){
    if(x.y!=y.y)
        return x.y<y.y;
    if(x.m!=y.m)
        return x.m<y.m;
    if(x.d!=y.d)
        return x.d<y.d;
    return x.s>y.s;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%s%d%d%d",&a[i].s,&a[i].y,&a[i].m,&a[i].d);
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;++i){
        printf("%s\n",a[i].s);
    }
    return 0;
}

用的冒泡 64

#include<bits/stdc++.h>
using namespace std;
struct Node{
    char s[25];
    int y,m,d;
}a[105];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%s%d%d%d",&a[i].s,&a[i].y,&a[i].m,&a[i].d);
    }
    for(int i=1;i<n;++i){
        for(int j=1;j<=n-i;++j){
            if(a[j].y>a[j+1].y)
                swap(a[j],a[j+1]);
            else if(a[j].y==a[j+1].y){
                if(a[j].m>a[j+1].m)
                    swap(a[j],a[j+1]);
                else if(a[j].m==a[j+1].m){
                    if(a[j].d>=a[j+1].d)
                        swap(a[j],a[j+1]);
                }
            }
        }
    }
    for(int i=1;i<=n;++i){
        printf("%s\n",a[i].s);
    }
    return 0;
}

by Handezheng @ 2024-07-12 09:08:51

@danaqi_ sort排序中
a[i].id 没赋值
输入时加上a[i].id = i


by danaqi_ @ 2024-07-12 09:12:16

@Handezheng
蟹蟹泥


by Handezheng @ 2024-07-12 09:12:19

#include<bits/stdc++.h>
using namespace std;
struct Node{
    char s[25];
    int y,m,d,id;
};
Node a[105];
bool cmp(Node x,Node y){
    if(x.y!=y.y)
        return x.y<y.y;
    if(x.m!=y.m)
        return x.m<y.m;
    if(x.d!=y.d)
        return x.d<y.d;
    return x.s>y.s;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%s%d%d%d",&a[i].s,&a[i].y,&a[i].m,&a[i].d);
      a[i].id = i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;++i){
        printf("%s\n",a[i].s);
    }
    return 0;
}

by danaqi_ @ 2024-07-12 09:18:06

@Handezheng
如果按你这个方法的话 id实际上没有用到

#include<bits/stdc++.h>
using namespace std;
struct Node{
    char s[25];
    int y,m,d,id;
};
Node a[105];
bool comp(Node x,Node y){
    if(x.y!=y.y)
        return x.y<y.y;
    if(x.m!=y.m)
        return x.m<y.m;
    if(x.d!=y.d)
        return x.d<y.d;
    if(x.id!=y.id)
        return x.id>y.id;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%s%d%d%d",&a[i].s,&a[i].y,&a[i].m,&a[i].d);
        a[i].id=i;
    }
    sort(a+1,a+n+1,comp);
    for(int i=1;i<=n;++i){
        printf("%s\n",a[i].s);
    }
    return 0;
}

像这样写 在第14行 id判断一下就AC了


by Handezheng @ 2024-07-12 09:22:30

咳咳..
你相信我是自己写了代码AC,但是给你发的是复制了你的然后给id赋上值就发你了吗?

所以就是,忘改sort了(抱歉)


by xcy135 @ 2024-07-15 12:45:16

坐下!!!


by danaqi_ @ 2024-10-20 13:31:35

@xcy135
烧饼


|