历届试题 日期问题

题目:

  历届试题 日期问题  
时间限制:1.0s   内存限制:256.0MB
    
问题描述
  小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在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
数据规模和约定
  峰值内存消耗(含虚拟机) < 256M
  CPU消耗 < 1000ms




  请严格按要求输出,不要画蛇添足地打印类似:“请您输入...” 的多余内容。


  注意:
  main函数需要返回0;
  只使用ANSI C/ANSI C++ 标准;
  不要调用依赖于编译环境或操作系统的特殊函数。
  所有依赖的函数必须明确地在源文件中 #include <xxx>
  不能通过工程设置而省略常用头文件。


  提交程序时,注意选择所期望的语言类型和编译器类型。

题解:我感觉这样的题可以锻炼小细节问题,细心的人厉害了

笨笨的代码:

#include<stdio.h>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int panduan(int x,int y,int z)
{
    int flag=0;
    if(x>=60)
        x=1900+x;
    else
    x=2000+x;
    if((x%4==0&&x%100!=0)||x%400==0)
        flag=1;
    if(flag==0&&y==2&&z==29)
    return 0;
       else
    {
         if(y>=1&&y<=12&&z>=1&&z<=30)
        return 1;
    else if((y==1||y==3||y==5||y==7||y==8||y==10||y==12)&&
            z==31)
    return 1;

    else
        return 0;
    }

}
void shuchu(int x1,int x2,int x3)
{
    if(x1>=60)
        printf("%d%02d",19,x1);
    else
        printf("%d%02d",20,x1);
    printf("%c%02d%c%02d\n",'-',x2,'-',x3);
}
int main()
{
    int a[3];
    string str;
    while(cin>>str)
    {
        a[0]=(str[0]-'0')*10+(str[1]-'0');
        a[1]=(str[3]-'0')*10+(str[4]-'0');
        a[2]=(str[6]-'0')*10+(str[7]-'0');

        if(a[0]==a[1]==a[2])
        {
            if(panduan(a[0],a[1],a[2])==1)
            {
                shuchu(a[0],a[1],a[2]);
            }
        }
        else if(a[1]==a[0])
        {
            if(a[0]<a[2])
            {
                if(panduan(a[0],a[1],a[2])==1)
                {
                    shuchu(a[0],a[1],a[2]);
                }
                if(panduan(a[2],a[0],a[1])==1)
                {
                    shuchu(a[2],a[0],a[1]);
                }
            }
          else
          {
              if(panduan(a[2],a[0],a[1])==1)
                {
                    shuchu(a[2],a[0],a[1]);
                }
                if(panduan(a[0],a[1],a[2])==1)
                {
                    shuchu(a[0],a[1],a[2]);
                }
          }
        }
        else
        {
           if(a[0]<a[2])
           {
               if(panduan(a[0],a[1],a[2])==1)
                {
                    shuchu(a[0],a[1],a[2]);
                }
              if(a[0]<a[1])
              {
                    if(panduan(a[2],a[0],a[1])==1)
                    {
                        shuchu(a[2],a[0],a[1]);
                    }
                    if(panduan(a[2],a[1],a[0])==1)
                    {
                        shuchu(a[2],a[1],a[0]);
                    }
              }
              else if(a[1]<a[0])
              {
                   if(panduan(a[2],a[1],a[0])==1)
                    {
                        shuchu(a[2],a[1],a[0]);
                    }
                    if(panduan(a[2],a[0],a[1])==1)
                    {
                        shuchu(a[2],a[0],a[1]);
                    }
              }

           }
           else if(a[2]<a[0])
           {
               if(a[0]<a[1])
               {
                   if(panduan(a[2],a[0],a[1])==1)
                    {
                        shuchu(a[2],a[0],a[1]);
                    }
                    if(panduan(a[2],a[1],a[0])==1)
                    {
                        shuchu(a[2],a[1],a[0]);
                    }
               }
               else
               {
                   if(panduan(a[2],a[1],a[0])==1)
                    {
                        shuchu(a[2],a[1],a[0]);
                    }
                     if(panduan(a[2],a[0],a[1])==1)
                    {
                        shuchu(a[2],a[0],a[1]);
                    }
               }
               if(panduan(a[0],a[1],a[2])==1)
                {
                    shuchu(a[0],a[1],a[2]);
                }
           }

        }

    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值