文章

日期问题(2017省赛)

日期问题1.png

结构体排序

#include<bits/stdc++.h>
using namespace std;
int day[20]={1,31,28,31,30,31,30,31,31,30,31,30,31};
int cnt=0;
struct stu{
    int y;
    int m;
    int d;
}res[1000];
int cmp(stu t1,stu t2){
    if(t1.y!=t2.y) return t1.y<t2.y;
    if(t1.m!=t2.m) return t1.m<t2.m;
    if(t1.d!=t2.d) return t1.d<t2.d;
    return  t1.d<=t2.d;
}
void year(int x){
    if((x%4==0&&x%100!=0)||x%400==0){
        day[2]=29;
    }else day[2]=28;
    return ;
}
void check(int a,int b,int c){
    if(a<=59) a+=2000;
    else a+=1900;
    year(a);
    if(b>=1&&b<=12&&c<=day[b]&&c>=1){
        res[cnt].y=a;
        res[cnt].m=b;
        res[cnt].d=c;
        cnt++;
    }
    return ;
}
void show(){
    for(int i=0;i<cnt;i++){
        if(i!=0&&res[i].y==res[i-1].y&&res[i].m==res[i-1].m&&res[i].d==res[i-1].d) continue;
        cout<<res[i].y<<'-';
        if(res[i].m<10)cout<<0;
        cout<<res[i].m<<'-';
        if(res[i].d<10) cout<<0;
        cout<<res[i].d<<endl;
    }
    return ;
}
int main()
{
    int a,b,c;
    string s;
    cin>>s;
    check(int((s[0]-'0')*10+s[1]-'0'),int((s[3]-'0')*10+s[4]-'0'),int((s[6]-'0')*10+s[7]-'0'));
    check(int((s[6]-'0')*10+s[7]-'0'),int((s[0]-'0')*10+s[1]-'0'),int((s[3]-'0')*10+s[4]-'0'));
    check(int((s[6]-'0')*10+s[7]-'0'),int((s[3]-'0')*10+s[4]-'0'),int((s[0]-'0')*10+s[1]-'0'));
    sort(res,res+cnt,cmp);
    show();
    return 0;
 } 

原题链接

https://www.lanqiao.cn/problems/103/learning/

License:  CC BY 4.0