G. February 29(时间推算问题)
Time Limit: 1000ms
Memory Limit: 131072KB
64-bit integer IO format: %lld Java class name: Main
Submit Status
G |
February 29 |
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
The first line of input will contain T (≤ 500) denoting the number of 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
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; int i,num; char month1[20],month2[20]; int day1,year1,day2,year2; int leapyear(int year) { if((year%4==0&&year%100!=0)||year%400==0) return 1; return 0; } int months(char *month) { if(strcmp(month,"January")==0) return 1; else if(strcmp(month, "February")==0) return 2; else if(strcmp(month,"March")==0) return 3; else if(strcmp(month,"April")==0) return 4; else if(strcmp(month,"May")==0) return 5; else if(strcmp(month,"June")==0) return 6; else if(strcmp(month,"July")==0) return 7; else if(strcmp(month,"August")==0) return 8; else if(strcmp(month,"September")==0) return 9; else if(strcmp(month,"October")==0) return 10; else if(strcmp(month,"November")==0) return 11; else if(strcmp(month, "December")==0) return 12; } int main() { int k=0; int t; scanf("%d",&t); while(t--) { k++; num=0; scanf("%s %d, %d",month1,&day1,&year1); scanf("%s %d, %d",month2,&day2,&year2); int m1=months(month1); int m2=months(month2); int dy1=year1/4-year1/100+year1/400; int dy2=year2/4-year2/100+year2/400; if(leapyear(year1)&&leapyear(year2)) { if(m1>2) dy1++; if(m2>2||(m2==2&&day2==29)) dy2++; } else if(!leapyear(year1)&&leapyear(year2)) { dy1++; if(m2>2||(m2==2&&day2==29)) dy2++; } else if(leapyear(year1)&&!leapyear(year2)) { dy2++; if(m1>2) dy1++; } printf("Case %d: %d\n",k,dy2-dy1); } }