Lightoj 1414 - February 29 (闰年统计,容斥定理)

来源:互联网 发布:社交网络的利弊 英文 编辑:程序博客网 时间:2024/05/17 06:04

http://lightoj.com/volume_showproblem.php?problem=1414
1414 - February 29
   PDF (English)StatisticsForum
Time Limit: 1 second(s)Memory Limit: 32 MB

It is 2012, and it's a leap year. So there is a "February 29" in this year, which is called leap day. Interesting thing is the infant who will born in this February 29, will get his/her birthday again in 2016, which is another leap year. So February 29 only exists in leap years. Does leap year comes in every 4 years? Years that are divisible by 4 are leap years, but years that are divisible by 100 are not leap years, unless they are divisible by 400 in which case they are leap years.

In this problem, you will be given two different date. You have to find the number of leap days in between them.

Input

Input starts with an integer T (≤ 550), denoting the number of test cases.

Each of the test cases will have two lines. First line represents the first date and second line represents the second date. Note that, the second date will not represent a date which arrives earlier than the first date. The dates will be in this format - "month day, year", See sample input for exact format. You are guaranteed that dates will be valid and the year will be in between 2 * 103 to 2 * 109. For your convenience, the month list and the number of days per months are given below. You can assume that all the given dates will be a valid date.

Output

For each case, print the case number and the number of leap days in between two given dates (inclusive).

Sample Input

Output for Sample Input

4

January 12, 2012

March 19, 2012

August 12, 2899

August 12, 2901

August 12, 2000

August 12, 2005

February 29, 2004

February 29, 2012

Case 1: 1

Case 2: 0

Case 3: 1

Case 4: 3

Note

The names of the months are {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" and "December"}. And the numbers of days for the months are {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31} respectively in a non-leap year. In a leap year, number of days for February is 29 days; others are same as shown in previous line.


PROBLEM SETTER: MD. ARIFUZZAMAN ARIF
SPECIAL THANKS: JANE ALAM JAN

题意:

在给定的日期里面,统计出闰年的个数。

思路:

这就尴尬了,模拟好久卡死了,最后才发现用容斥定理,数论暂时没看到,这里参看了学长的博客。


关于容斥定理的讲解 -> 百度百科链接

AC CODE:

#include<stdio.h>#include<cstring>#include<map>/*-------------map映射的学习-----------------*/#include<string>#include<algorithm>#define HardBoy main()#define ForMyLove return 0;using namespace std;const int MYDD = 1103;char dd[16][32] = {"0", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};int Leap(int year) {//闰年的容斥return year/4 + year/400 - year/100 ;}int HardBoy {int tt, kc = 1;scanf("%d", &tt);map<string, int> month;for(int j = 1; j <= 12; j++) {month[dd[j]] = j;/* 月份单词映射为数字 */}while(tt--) {char kai[32], e[32];int m1, m2, d1, d2, y1, y2;scanf("%s %d, %d", kai, &d1, &y1);m1 = month[kai];scanf("%s %d, %d", e, &d2, &y2);m2 = month[e];if(m1 > 2)y1++;if(m2 < 2 || m2==2 && d2 < 29) y2--;int ans = Leap(y2) - Leap(y1-1);printf("Case %d: %d\n", kc++, ans);}ForMyLove}


0 0
原创粉丝点击