【HDOJ】2133 What day is it

需要注意数据有效性。

 1 #include <stdio.h>
 2 
 3 #define isLeapYear(y) (y%4==0&&y%100!=0)||(y%400==0)
 4 
 5 int nums[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}};
 6 char buf[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 7 
 8 int main() {
 9     int i, j, year, month, day, total;
10 
11     while (scanf("%d %d %d", &year, &month, &day) != EOF) {
12         total = day;
13         j = isLeapYear(year);
14         if (month==0 || day==0) {
15             printf("illegal\n");
16             continue;
17         }
18         for (i=1; i<month; ++i)
19             total += nums[j][i];
20         if (month == 2) {
21             if (!j && day>28) {
22                 printf("illegal\n");
23                 continue;
24             }
25             if (j && day>29) {
26                 printf("illegal\n");
27                 continue;
28             }
29         }
30         if ((month==4||month==6||month==9||month==11) && day>30) {
31             printf("illegal\n");
32             continue;
33         }
34         for (i=1; i<year; ++i) {
35             if (isLeapYear(i))
36                 total += 366;
37             else
38                 total += 365;
39         }
40         i = total % 7;
41         printf("%s\n", buf[i]);
42     }
43 
44     return 0;
45 }

 

posted on 2014-04-24 22:31  Bombe  阅读(237)  评论(0编辑  收藏  举报

导航