2018年四川省省赛 E题Ever(闰年的判断)

题目:

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.
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.

    首先先在这里批评下自己吧,竟然在比赛的时候忘记了怎么判断闰年。(水,不是一般的水,所以惩罚自己从现在开始到考试结束每天都必须发一条博客,有关基础内容的。)

        下面来进入正题:

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;
}
欢迎大家来看我的博客,希望我写的内容能够帮助到你,还望大家多多指教!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值