前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2018四川省大学程序设计竞赛(ACM) E: Ever17 ----------------------C语言——菜鸟级

2018四川省大学程序设计竞赛(ACM) E: Ever17 ----------------------C语言——菜鸟级

作者头像
Fivecc
发布2022-11-21 15:04:35
2410
发布2022-11-21 15:04:35
举报
文章被收录于专栏:前端ACE

Description As is known to all, we have two formats to denote a specific date: MM/DD/YY or YY/MM/DD. We supposed the years discussed below are years in 20YY.

Now you are given a string representing the date. If there is only one possible date it can fit in with format of “MM/DD/YY” or “YY/MM/DD”, output the date in the format of “month date, year” (as can be seen in the second sample). Otherwise, output the days between the two different dates it might be.

Input The first line contains an integer T T representing the number of test cases. In each test case, a string ”AA/BB/CC” is given in one line. It is guaranteed that there is at least one legal date it can fit in with format of ”MM/DD/YY” or ”YY/MM/DD”. 1≤T≤5 1≤T≤5

Output For each test case, if there are two different possible dates, output an integer in one line representing the days between the two possible dates. In the other case, output the exact date in one line.

Sample Input

Raw

3 02/07/19 19/02/07 07/02/19

Sample Output

Raw

6047 February 7, 2019 4516

Hint In the first sample, the date might be July 19th, 2002 or February 7th, 2019. The number of days between the two dates are 6047 days. As for the second sample, the only possible date is February 7th, 2019. There are 12 months in a year and they are January, February, March, April, May, June, July, August, September, October, November and December. Also, please note that there are 29 days in February in a leap year while 28 days in nonleap year.

重在细节!!!!!!

AC代码

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
int T,aa,bb,cc,flag1,flag2;
int p[13]={29,31,28,31,30,31,30,31,31,30,31,30,31};
int pdrn(int yy)//闰年判断 
{if((yy%4==0&&yy%100!=0)||yy%400==0)return 1;
 else return 0;
}
int gs1()//判断 是否 符合 格式 1  MM/DD/YY 
{ int yy=cc+2000;p[2]=28;
    if(pdrn(yy))p[2]=p[0];
    if(aa>12||aa==0)return 0;
    if(bb>p[aa]||bb==0)return 0;
    return 1;
}
int gs2()//判断 是否 符合 格式 2  YY/MM/DD 
{ int yy=aa+2000;p[2]=28;
    if(pdrn(yy))p[2]=p[0];
    if(bb>12||bb==0)return 0;
    if(cc>p[bb]||cc==0)return 0;
    return 1;
}
int tsc()//求天数差  =(起始年到目标日期-起始年到起始日期)  
{      
    int y1=aa+2000,m1=bb,d1=cc,y2=cc+2000,m2=aa,d2=bb,ans=0,i,sum=0;
      //y1 m1 d1 为起始日期    y2 m2 d2 为目标日期 
    if(aa>cc)y1=cc+2000,m1=aa,d1=bb,y2=aa+2000,m2=bb,d2=cc;//较大的日期作为目标日期 
    p[2]=28;//默认 2月为平年 
    if(pdrn(y1))p[2]=29;//若为闰年 附值为29 
    for(i=1;i<m1;i++)//计算 起始年(XXXX年1月1日) 到 起始日期的天数 
        sum+=p[i];
        sum+=d1;
    while(y1<y2)//以年为单位在增加 
    {
        if(pdrn(y1))ans+=366;
        else ans+=365;
        y1++;
    }
    p[2]=28;
    if(pdrn(y2))p[2]=p[0]; 
    for(i=1;i<m2;i++)//以月为单位增加 
        ans+=p[i];
        ans+=d2;//天数增加 
        return ans-sum;//(起始年到目标日期-起始年到起始日期) 
}
int main()
{  char yue[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    scanf("%d",&T);
    while(T--)
    {   
        scanf("%d/%d/%d",&aa,&bb,&cc);
        flag1=gs1();
         flag2=gs2();
        if(flag1&&flag2)
        {
         if(aa==bb&&bb==cc)printf("%s %d, %d\n",yue[aa-1],bb,2000+cc);
         else 
        {int ans=tsc();if(ans<0)ans=-ans;//01/03/01同一年的情况可能为负 
            printf("%d\n",ans);
        }
        }else
        {   if(flag2)printf("%s %d, %d\n",yue[bb-1],cc,2000+aa);
            else printf("%s %d, %d\n",yue[aa-1],bb,2000+cc);
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档