acwing 1229. 日期问题(蓝桥杯)

题目:1229. 日期问题

在这里插入图片描述

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int a[5],ct=0;
int mon[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x){
    if(x<19600101||x>20591231) return false;
    int m=x%10000/100;
    int d=x%100;
    int y=x/10000;
    if(m<=0||m>=13||d<=0) return false;
    if(m==2){
        if((y%4==0)&&(y%100!=0)||y%400==0){
            if(mon[2]+1<d) return false;
        }else{
            if(mon[2]<d) return false;
        }
    }else {
        if(mon[m]<d) return false;
    }
    return true;
}
void get(int x,int y,int z){
    if(x<=59) x=2000+x;
    else x=1900+x;
    int t=x*10000+y/10*1000+y%10*100+z/10*10+z%10;
    if(check(t)){
        a[ct++]=t;
    }
}
int main(){
    int x,y,z;
    scanf("%d/%d/%d",&x,&y,&z);
    get(x,y,z);
    get(z,y,x);
    get(z,x,y);
    sort(a,a+ct);
    for(int i=0;i<ct;i++){
        int t=a[i];
        printf("%d-%02d-%02d\n",t/10000,t%10000/100,t%100);

    }
    return 0;
}

下面的代码,我是想使用一下unique()和erase()结合去重

#include<bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
vector<int >a;
int mon[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int x){
    if(x<19600101||x>20591231) return false;
    int m=x%10000/100;
    int d=x%100;
    int y=x/10000;
    if(m<=0||m>=13||d<=0) return false;
    if(m==2){
        if((y%4==0)&&(y%100!=0)||y%400==0){
            if(mon[2]+1<d) return false;
        }else{
            if(mon[2]<d) return false;
        }
    }else {
        if(mon[m]<d) return false;
    }
    return true;
}
void get(int x,int y,int z){
    if(x<=59) x=2000+x;
    else x=1900+x;
    int t=x*10000+y/10*1000+y%10*100+z/10*10+z%10;
    if(check(t)){
        a.push_back(t);
    }
}
int main(){
    int x,y,z;
    scanf("%d/%d/%d",&x,&y,&z);
    get(x,y,z);
    get(z,y,x);
    get(z,x,y);

    sort(a.begin(),a.end());
//        int b[5],b_ct=0;
//        b[0]=a[0];
//        for(int i=1;i<ct;i++){
//            if(a[i]!=a[i-1]) b[++b_ct]=a[i];
//        }
    a.erase(unique(a.begin(),a.end()),a.end());
    for(int i=0;i<a.size();i++){
        int t=a[i];
        printf("%d-%02d-%02d\n",t/10000,t%10000/100,t%100);

    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值