2018年 ACM四川省赛 Problem E-Ever17 _c++代码

本文介绍了一个关于日期格式处理的问题,探讨了如何解析不确定格式的日期字符串,并将其转换为明确的日期格式,同时提供了两种输出结果:单一明确日期或两个可能日期间的天数差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

The first line contains an integer 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”. 1T5
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.
302/07/1919/02/0707/02/19
6047February 7, 20194516
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.


#include<string>
#include<cstdio>
#include<cstdlib>

using namespace std;

string mm[]= {"","January","February","March","April","May","June","July","August","September","October","November","December"};
int cm[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};

bool isdate(int y,int m,int d) {
    return m>0&&m<=12&&d>0&&(y%4==0&&m==2?d<=29:d<=cm[m]);
}

int days(int y,int m,int d) {
    int cnt_m=0;
    for(int i=1; i<m; i++)cnt_m+=(i==2&&y%4==0?29:cm[i]);
    return (y-1)/4+1 + y*365 + d + cnt_m;
}

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int a0,a1,a2;
        scanf("%d/%d/%d",&a0,&a1,&a2);
        int cnt=0;
        if(isdate(a0,a1,a2)) cnt++;
        if(isdate(a2,a0,a1)&&!(a0==a1&&a1==a2)) cnt++;
        if(cnt==1) {
            int y,m,d;
            if(isdate(a0,a1,a2)) { y=a0; m=a1; d=a2;} 
            else  { y=a2; m=a0; d=a1;}
            printf("%s %d, %d\n",mm[m].c_str(),d,2000+y);
        } else if(cnt==2) {
            printf("%d\n",abs(days(a0,a1,a2)-days(a2,a0,a1)));
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值