openjudge------ 日期的种类题目

描述
TXT is a vegetable chicken,so 出题什么的完全不会啊!

干脆直接从网络上copy一题下来吧。

小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是,这些日期采用的格式非常不统一,有采用年/月/日的,有采用月/日/年的,还有采用日/月/年的。更加麻烦的是,年份也都省略了前两位,使得文献上的一个日期,存在很多可能的日期与其对应。

比如02/03/04,可能是2002年03月04日、2004年02月03日或2004年03月02日。

给出一个文献上的日期,你能帮助小明判断有哪些可能的日期对其对应吗?

输入
一个日期,格式是"AA/BB/CC"。 (0 <= A, B, C <= 9)
输出
输出若干个不相同的日期,每个日期一行,格式是"yyyy-MM-dd"。多个日期按从早到晚排列。
样例输入
02/03/04
样例输出
2002-03-04
2004-02-03
2004-03-02

 

思路:

因为日期只有三类,所以只需要分成三类讨论,然后对每一种结果判断是否符合,并且排序后输出就可以了。

#include<stdio.h>
#include<string.h>
int one[7]={1,3,5,7,8,10,12};
int two[4]={4,6,9,11};
int get(char ch[],int aa,int bb){
    return (ch[aa]-'0')*10+ch[bb]-'0';
}
int run(int n){
     if((n%4 == 0&&n%100 != 0)||(n%400 == 0)){
            return 1;
     }
     return 0;
}
int in(int n,int a[],int len){
    int i;
    for(i=0;i<len;i++){
        if(a[i]==n){
            return 1;
        }
    }
    return 0;
}
int can(int a,int b,int c){  //检查以a,b,c为年月日合法不 
    int temp;
    if(a>=60){
        temp = 1900+a;
    }else{
        temp = 2000+a;
    }
    if(b>=1&&b<=12){
           if(c>=1&&c<=31){
                if((c==29&&!(run(temp)&&b==2))||(c==28&&!(!run(temp)&&b==2))){
                    return 0;
                }
                else if(c==31&&!in(b,one,7)){
                    return 0;
                }
                else if(c==30&&!in(b,two,4)){
                    return 0;
                }else{
                    return 1;
                }
           }else{
                return 0;
           }
       }else{
            return 0;
    }
}
void change(int &a,int &b,int &c){
    int max = a>b?a:b;
    int min = a>b?b:a;
    max = max>c?max:c;
    min = min>c?c:min;
    b = a+b+c-max-min;
    a = min;
    c = max;
}
int main(){
    char str[10];
    gets(str);
    int a,b,c,t1=0,t2=0,t3=0;
    a = get(str,0,1);
    b = get(str,3,4);
    c = get(str,6,7);
    if(can(a,b,c)){
        if(a>=60)
        t1 = 19000000+a*10000+b*100+c;
        else
        t1 =  20000000+a*10000+b*100+c;
    }
    if(can(c,a,b)){ 
        if(c>=60)
            t2 = 19000000+c*10000+a*100+b;
        else
        t2 = 20000000+c*10000+a*100+b;
    }
    if(can(c,b,a)){
        if(c>=60)
            t3 = 19000000+c*10000+b*100+a;
        else
        t3 = 20000000+c*10000+b*100+a;
    }
    if(a==b){
        t3 = 0;
    }
    if(a==b&&b==c){
        t2 = 0;
    }
    change(t1,t2,t3);
    if(t1)
        printf("%0.2d-%0.2d-%0.2d\n",t1/10000,t1%10000/100,t1%100);
    if(t2)
    printf("%0.2d-%0.2d-%0.2d\n",t2/10000,t2%10000/100,t2%100);
    if(t3)
    printf("%0.2d-%0.2d-%0.2d\n",t3/10000,t3%10000/100,t3%100);
    return 0;
} 

 

转载于:https://www.cnblogs.com/Koaler/p/7211218.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值