百练 2964 日历问题 解题报告

1.链接:http://poj.grids.cn/practice/2964/

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
在我们现在使用的日历中, 闰年被定义为能被4整除的年份,但是能被100整除而不能被400整除的年是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。 给定从公元2000年1月1日开始逝去的天数,你的任务是给出这一天是哪年哪月哪日星期几。
输入
输入包含若干行,每行包含一个正整数,表示从2000年1月1日开始逝去的天数。输入最后一行是−1, 不必处理。可以假设结果的年份不会超过9999。
输出
对每个测试样例,输出一行,该行包含对应的日期和星期几。格式为“YYYY-MM-DD DayOfWeek”, 其中 “DayOfWeek” 必须是下面中的一个: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" 或 "Saturday“。
样例输入
1730 
1740 
1750 
1751 
-1
样例输出
2004-09-26 Sunday 
2004-10-06 Wednesday 
2004-10-16 Saturday 
2004-10-17 Sunday
提示
2000.1.1. 是星期六

3.代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 int days_of_year[2] = {365,366};
 9 int days_of_month[24] = {31,28,31,30,31,30,31,31,30,31,30,31,/**/31,29,31,30,31,30,31,31,30,31,30,31};
10 char days_of_week[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ,"Saturday"};
11 
12 int main()
13 {
14      int day;
15      int start_year,start_month,start_day,start_week;
16     int is_leap_year;
17      while(scanf("%d",&day),day!=-1)
18      {
19         start_year = 2000;
20         start_month = 1;
21         start_day = 1;
22         start_week = 6;
23         
24         
25         start_week = (start_week + day) % 7;
26         
27         //判断是否闰年
28         if(start_year % 4 == 0 && !(start_year % 100 == 0 && start_year % 400 != 0))
29         {
30             is_leap_year = 1;
31         }
32         else
33         {
34             is_leap_year = 0;
35         }
36         
37         while(day >= days_of_year[is_leap_year])
38         {
39             start_year ++;
40             day -= days_of_year[is_leap_year];
41             
42             //判断是否闰年
43             if(start_year % 4 == 0 && !(start_year % 100 == 0 && start_year % 400 != 0))
44             {
45                 is_leap_year = 1;
46             }
47             else
48             {
49                 is_leap_year = 0;
50             }
51         }
52         
53         while(day >= days_of_month[is_leap_year*12 + start_month - 1])
54         {    
55             day -= days_of_month[is_leap_year*12 + start_month - 1];
56             start_month ++;
57         }
58         
59         start_day += day;
60         
61         
62         printf("%d-%02d-%02d %s
",start_year,start_month,start_day,days_of_week[start_week]);
63      }
64      return 0;
65 }

4.思路

(1)首先要知道闰年的相关知识

(2)模拟每年每月的计算过程

原文地址:https://www.cnblogs.com/mobileliker/p/3154661.html