HDU 1308 What Day Is It?(模拟,日期)

解题报告:输入一个年月日,让你求出那一天是星期几,但是做这题之前必须先了解一点历史。首先在1582年之前,判断是否是闰年的标准是只要能被四整除就是闰年,

然后在1752年9月2号的后的11天被抹去了,也就是说1752年9月2号的第二天不是9月3号,而是9月14号。然后知道这些这题就好做了,我的做法是把1年1月1号作为

参考日,那天是星期天,然后计算输入的日子与那天差多少天就可以判断出输入的日子是星期几了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 int m[2][13] = {
 8 {0,31,28,31,30,31,30,31,31,30,31,30,31},
 9 {0,31,29,31,30,31,30,31,31,30,31,30,31},
10 };
11 char xingqi[8][20] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
12 char yue[13][20] = {"","January","February","March","April","May","June","July","August","September","October","November","December"};
13 
14 int judge(int year)   //判断是否为润年
15 {
16     if(year > 1582)
17     return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
18     else return year % 4 == 0;
19 }
20 
21 int num_day(int mon,int day,int year)      //以公元1年1月1日为参考点,求到输入的日期为止已经过去的天数
22 {
23     int tot = 0;
24     for(int i = 1;i < year;++i)
25     tot += (judge(i)? 366:365);
26     for(int i = 1;i < mon;++i)
27     tot += m[judge(year)][i];
28     tot += day;
29     return tot;
30 }
31 int panduan(int mon,int day,int year)   //判断输入的日期是否合法
32 {
33     if(mon <= 0 || day <= 0 || year <= 0)
34     return 0;
35     if(mon > 12)
36     return 0;
37     if(day > m[judge(year)][mon])
38     return 0;
39     return 1;
40 }
41 
42 int main()
43 {
44     int day,mon,year;
45     while(scanf("%d%d%d",&mon,&day,&year))
46     {
47         if(day == mon && mon == year && year == 0)
48         break;
49         int tot_d = num_day(mon,day,year);
50         if(tot_d > 639798)
51         tot_d -= 11;
52         tot_d -= 1;
53         if(panduan(mon,day,year))
54         printf("%s %d, %d is a %s\n",yue[mon],day,year,xingqi[tot_d % 7]);
55         else printf("%d/%d/%d is an invalid date.\n",mon,day,year);
56     }
57     return 0;
58 }

 

posted @ 2017-07-16 15:20  Roni_i  阅读(272)  评论(0编辑  收藏  举报