48分求助

P1104 生日

hjsxzz2022 @ 2024-02-17 20:57:20


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n; 
bool isrun(int y){
    if((y%4==0&&y%100!=0)||y%400==0)
        return true;
    return false;
}
int month(int y,int m){
    if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
        return 31;
    }else if(m==4||m==6||m==9||m==11){
        return 30;
    }else{
        if(isrun(y)){
            return 29;
        }else{
            return 30;
        }
    }
}

struct bd{
    string s;
    int y,m,d;
    long long sd=0;
}t[101];
bool cmd(bd a,bd b){
    if(a.sd<b.sd)
        return true;
    return false;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>t[i].s>>t[i].y>>t[i].m>>t[i].d;
        for(int j=1;j<t[i].y;j++){
            if(isrun(j))
                t[i].sd+=366;
            else
                t[i].sd+=365;
        }
        for(int j=1;j<=t[i].m;j++){
            t[i].sd+=month(t[i].y,t[i].m);
        }
        t[i].sd+=t[i].d;
    }
    sort(t,t+n,cmd);
    for(int i=0;i<n;i++){
        cout<<t[i].s<<endl;
    }
    return 0;
}

by a_study_xxs @ 2024-10-28 21:01:17

没有必要像你那么做啊!


by a_study_xxs @ 2024-10-28 21:05:50


#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;
}

|