February 29(模拟)

D - D
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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

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

Sample Output

Case 1: 1

Case 2: 0

Case 3: 1

Case 4: 3

题解:给两个日期,找在之间的2月29号的个数;wa了9次,所幸过了。。。

 the second date will not represent a date which arrives earlier than the first date代表第二个日期比第一个大;

代码:

  1 #include<cstdio>
  2 #include<set>
  3 #include<queue>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<map>
  7 #include<string>
  8 using namespace std;
  9 bool js(int y,int r, int yy, int rr){
 10     if(y < yy)return true;
 11     if(y > yy)return false;
 12     if(r <= rr)return true;
 13     return false;
 14 }
 15 bool rn(int y){
 16     if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
 17         return true;
 18     return false;
 19 }
 20 map<string, int>mp;
 21 void init(){
 22     mp["January"] = 1;
 23     mp["February"] = 2;
 24     mp["March"] = 3;
 25     mp["April"] = 4;
 26     mp["May"] = 5;
 27     mp["June"] = 6;
 28     mp["July"] = 7;
 29     mp["August"] = 8;
 30     mp["September"] = 9;
 31     mp["October"] = 10;
 32     mp["November"] = 11;
 33     mp["December"] = 12;
 34 }
 35 void work(int &y1,int &y2,int &m1,int &m2,int &d1,int &d2){
 36     if(y1 > y2){
 37         swap(y1,y2);
 38         swap(m1,m2);
 39         swap(d1,d2);
 40         return;
 41     }
 42     if(y1 < y1){
 43         return;
 44     }
 45     if(m1 > m2){
 46         swap(y1,y2);
 47         swap(m1,m2);
 48         swap(d1,d2);
 49         return;
 50     }
 51     if(m1 < m2){
 52         return;
 53     }
 54     if(d1 > d2){
 55         swap(y1,y2);
 56         swap(m1,m2);
 57         swap(d1,d2);
 58         return;
 59     }
 60 }
 61 int find(int y){
 62     int t, t1, t2;
 63     t = (y - 2000) / 4 + 1;
 64     t1 = (y - 2000) / 100 + 1;
 65     t2 = (y - 2000) / 400 + 1;
 66     return (t - t1 + t2);
 67 }
 68 int main(){
 69     int N, kase = 0;
 70     char s1[15], s2[15];
 71     int y1, d1, y2, d2, m1, m2;
 72     scanf("%d", &N);
 73     init();
 74     while(N--){
 75         scanf("%s%d,%d", s1, &d1, &y1);
 76         scanf("%s%d,%d", s2, &d2, &y2);
 77         if(mp.count(s1)){
 78             m1 = mp[s1];
 79         }
 80         else while(1);
 81         if(mp.count(s2)){
 82             m2 = mp[s2];
 83         }
 84         else while(1);
 85     //    work(y1,y2,m1,m2,d1,d2);
 86         if(y1 == y2){
 87             if(rn(y1) && js(m1,d1, 2, 29) && js(2, 29, m2, d2)){
 88                 printf("Case %d: %d
", ++kase, 1);
 89                 continue;
 90             }
 91             printf("Case %d: %d
", ++kase, 0);
 92             continue;
 93         }
 94         int t = find(y2 - 1) - find(y1);

 95         if(rn(y1) && js(m1,d1, 2, 29))t++;
 96         if(rn(y2) && js(2, 29, m2, d2))t++;
 97         printf("Case %d: %d
", ++kase, t);
 98     }
 99     return 0;
100 }
原文地址:https://www.cnblogs.com/handsomecui/p/5451096.html