第十届四川省大学生程序设计竞赛

点击打开链接

E: Ever17

Time Limit: 1000 MS Memory Limit: 1048576 KB
Total Submit: 196 Accepted: 39 Page View: 573
Submit Status Clarify

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 Trepresenting 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.
3
02/07/19
19/02/07
07/02/19
6047
February 7, 2019
4516
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.


题意:

给一个合法的日期,可能是MM/DD/YY or YY/MM/DD,如果是其中的一种,则输出"month date, year",

否则,输出两个日期的差值,若差值为0,输出个日期

Otherwise, output the days between the two different dates it might be.

#include<bits/stdc++.h>
using namespace std;
int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leap_day[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
char month[13][10]={"","January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" ,"December"};
bool leap(int y)
{
    if(y%4==0&&y%100!=0||y%400==0)
        return 1;
    return 0;
}
bool check(int y,int m,int d)
{
    if(y<0||y>99)   return 0;
    if(m<1||m>12)   return 0;
    if(d<1||d>31)   return 0;
    if(leap(y+2000))
    {
        if(d>leap_day[m])   return 0;
    }
    else
        if(d>day[m])   return 0;
    return 1;
}
long long sum(int y,int m,int d)
{
    long long sum=0;
    for(int i=0;i<y;i++)
    {
        if(leap(i+2000))
             sum+=366;
        else
            sum+=365;
    }
    if(leap(y+2000)){
         for(int i=1;i<m;i++)
       sum+=leap_day[i];
    }
    else
    {
        for(int i=1;i<m;i++)
            sum+=day[i];
    }
    sum+=d;
    return sum;
}
void print(int y,int m,int d)
{
    printf("%s %d, %d\n",month[m],d,y+2000);
}
int main()
{
    int t;
    int a,b,c;
    scanf("%d",&t);
    while(t--){
        scanf("%d/%d/%d",&a,&b,&c);
        bool s1=check(c,a,b),s2=check(a,b,c);
        if(s1&&!s2) print(c,a,b);
        if(!s1&&s2) print(a,b,c);
        if(s1&&s2)//两种情况下都合法
        if(abs(sum(c,a,b)-sum(a,b,c)))//差值不为0
            printf("%lld\n",abs(sum(c,a,b)-sum(a,b,c)));
        else//输出一个日期
            //print(a,b,c);
            print(c,a,b);
    }
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/80573649