微软编程之美资格赛——两个年份之间有几个2月29日

 1 #include<iostream>
 2 #include<string>
 3 #include<cmath>
 4 using namespace std;
 5 string Strmonth[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
 6 class date{
 7 public:
 8     string month;
 9     int day;
10     int year;
11     int imonth;
12 };
13 bool isleapyear(int year)   
14 {
15     if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
16         return 1;
17     else
18         return 0;
19 }
20 int select(int y1,int m1,int d1,int y2,int m2,int d2)
21 {  
22     int num = 0;
23     for (int i = y1 + 1; i < y2; i++)
24     {
25         if (isleapyear(i))
26         {
27             num++;
28         }
29     }
30     if (isleapyear(y1) && m1 <= 2)
31     {
32         num++;
33     }
34 
35     if (isleapyear(y2) &&(( m2 > 2)||(m2==2 && d2==29)))
36     {
37         
38         num++;
39     }
40     return num;
41 
42 }
43 int trans(string m){
44     int a;
45     for (int i = 0; i <= 11; i++)
46     {
47         if (m == Strmonth[i])
48             a = i;
49     }
50     return a + 1;
51 }
52 
53 int main(){
54     string m1, m2;
55     int n;
56     int count = 0;
57     int s = 0;
58     cin >> n;
59     while (n--)
60     {   
61         count = 0;
62         s++;
63         char t;
64         date start;
65         date end;
66         cin >> start.month;
67         cin >> start.day;
68         cin >> t;
69         cin >> start.year;
70         cin >> end.month;
71         cin >> end.day;
72         cin >> t;
73         cin >> end.year;
74         start.imonth= trans(start.month);
75         end.imonth = trans(end.month);
76         if (start.year == end.year && isleapyear(start.year))
77         {
78             if (start.imonth <2  && end.imonth>2)
79             {
80                 count++;
81             }
82             else if (start.imonth == 2 && start.day == 29)
83             {
84                 count++;
85             }
86             else if (end.imonth == 2 && end.day == 29)
87             {
88                 count++;
89             }
90         }
91         else if (start.year !=end.year)
92         {
93             count = select(start.year, start.imonth, start.day, end.year, end.imonth, end.day);
94         }
95         cout <<"Case #"<<s<<": "<< count << endl;
96 
97     
98     }
99 }

 

posted @ 2015-04-21 19:17  六层楼  阅读(275)  评论(0)    收藏  举报