What day is it

Description

Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

Input

There are multiply cases.        One line is one case.        There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

Output

Output one line.        if the date is illegal, you should output "illegal". Or, you should output what day it is.

Sample Input

2007 11 17

Sample Output

Saturday

注意这题细节很多,我是从0001年01月01日为周一考虑的。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 using namespace std;
 5 int a[]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 6 int b[]={0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 7 char c[8][11]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
 8 int panduan(int y)
 9 {
10     if((y%4==0 && y%100!=0) || y%400==0)
11         return 1;
12     return 0;
13 }
14 int main()
15 {
16    int y, m, d, sum, i;
17    while(cin>>y>>m>>d)
18    {
19       if(panduan(y))
20       {
21           if(d>b[m]||m==0||d==0)
22             {
23                 cout << "illegal" << endl;
24                 continue;
25             }
26       }
27       else
28         {
29             if(d>a[m]||m==0||d==0)
30             {
31                 cout << "illegal" << endl;
32                 continue;
33             }
34         }
35         sum=0;
36         for (i=1; i<y; ++i)
37             {
38                if(panduan(i))
39                 sum += 366;
40                else
41                 sum += 365;
42
43             }
44         for(int i = 0; i < m; ++i)
45         {
46             if(panduan(y))
47                 sum += b[i];
48             else
49                 sum += a[i];
50
51         }
52         sum+=d;
53         sum%=7;
54         cout<< c[sum] << endl;
55    }
56    return 0;
57 }
时间: 2024-10-10 20:23:00