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
Output
Hint
首先先在这里批评下自己吧,竟然在比赛的时候忘记了怎么判断闰年。(水,不是一般的水,所以惩罚自己从现在开始到考试结束每天都必须发一条博客,有关基础内容的。)
下面来进入正题:
1.判断闰年的条件:(口诀:四年一闰,百年不闰,四百年再闰。)
if( p % 400 == 0 || ( p % 100 != 0 && p % 4 == 0 ) )
return 366; //闰年
return 365; // 平年
2.仔细读题: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.
因此这里有一个坑点:当输入的样例为01/01/01等三个都相等的时候,不管怎么转换都是一样的两个数,故应该满足第一个条件,输出当前这个确切的日期。
3.解题思路:
由于这道题需要判断的地方比较多,全部写在主函数里面,容易出错,因此我采用了写额外的函数来执行这些任务,主函数来调用就可以了。
4.实现代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int check(int t)
{
if(t % 4 ==0 || (t % 100 != 0 && t % 400 == 0))
return 366;
return 365;
}
int pan(int a,int b,int c)
{
if(b > 0 && b <= 12 && c > 0 && (((b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12) && c <= 31) || ((b == 4 || b == 6 || b == 9 || b == 11) && c <= 30) || (check(a + 2000) == 366 && b == 2 && c <= 29) || c < 29))
return 1;
return 0;
}
int main()
{
int n,i,a,b,c,ans;
int yue[15] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
char s[15][15] = {"","January","February","March", "April", "May", "June", "July", "August", "September", "October", "November" , "December"};
scanf("%d",&n);
while(n--)
{
int w = 0,w1 = 0;
scanf("%d/%d/%d",&a,&b,&c);
if(pan(a,b,c) + pan(c,a,b) == 2)
{
for(i = 0;i < a; i++)
w += check(i + 2000);
for(i = 1;i < b; i++)
w += yue[i];
w += c;
if(check(2000 + a) == 366 && b > 2)
w++;
for(i = 0;i < c; i++)
w1 += check(i + 2000);
for(i = 1;i < a; i++)
w1 += yue[i];
w1 += b;
if(check(2000 + c) == 366 && a > 2)
w1++;
ans = abs(w - w1);
if(ans == 0)
printf("%s %d,%d\n",s[b],c,2000 + a);
else
printf("%d\n",ans);
}
else
{
if(pan(a,b,c) == 1)
printf("%s %d,%d\n",s[b],c,2000 + a);
else
printf("%s %d,%d\n",s[a],b,2000 + c);
}
}
return 0;
}