POJ 2080 Calendar (日期类题目)

前言:

这道题属于直接存储类线性表,主要就是数组,日期的题目也算比较常见吧,这道题没啥特别,但就是WA了三次,中间有些细节还是值得记录的,顺便总结一下,日期类型的题,可以开一个结构数组来存,或者同时开几个一维数组分别存年份,月份,日期等等。

上题:POJ 2080

Describe:

A calendar is a system for measuring time, from hours and minutes, to months and days,

and finally to years and centuries.

The terms of hour, day, month, year and century are all units of

time measurements of a calender system.
According to the Gregorian calendar, which is the civil calendar in use today,

years evenly divisible by 4 are leap years,

with the exception of centurial years that are not evenly divisible by 400.

Therefore, the years 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000,

and 2400 are leap years.
Given the number of days that have elapsed since January 1, 2000 A.D,

your mission is to find the date and the day of the week.

Input:

The input consists of lines each containing a positive integer,

which is the number of days that have elapsed since January 1, 2000 A.D.

The last line contains an integer −1, which should not be processed.
You may assume that the resulting date won’t be after the year 9999.

Output:

For each test case, output one line containing the date and the day

of the week in the format of "YYYY-MM-DD DayOfWeek",

where "DayOfWeek" must be one of "Sunday",

"Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".

Sample Input:
1730
1740
1750
1751
-1
Sample Output:
2004-09-26 Sunday
2004-10-06 Wednesday
2004-10-16 Saturday
2004-10-17 Sunday

题目大意:

给你一个天数n,计算出从2000年1月1日开始,经过n天,是几年几月几日,星期几,并按照格式输出。

解题思路:

写俩函数,一个计算n天里,从2000/1/1(星期六)开始有多少年,一个计算还剩下多少个月,正常按照思路写就可以了,一些细节写到代码注释里了。

小细节:

1. scanf前加个~相当于 scanf != EOF

2. 输出空位补零,printf(" %.md", n);  m为一个整数,表示占多少位,空位补零,如果没有‘ . ' 就是空位补空格

AC代码:

 

 1 #include 
 2 #include 
 3 using namespace std;
 4 int year,month,week,day,n;// 各种变量,比较好理解
 5 // 提前将字符串存入数组,用来计算星期几
 6 char s[7][10] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
 7 // 计算n天里存在多少年
 8 // 注意是否是闰年的判断方法
 9 void num_year()
10 {
11     while(n >= 365) // 这个范围,要注意,大于等于最小的,WA一次
12     {
13         if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
14         {
15             if(n-366>=0)  // 既然大于等于最小的,就要特判,是否为负
16             {
17                 n -= 366;
18                 year++;   // 注意:判断成立了再++,WA一次
19             } else break;
20         } else {
21             n -= 365;
22             year++;
23         }
24     }
25 }
26 // 判断n天里,有多少个月
27 // 注意点同上个函数
28 void num_month()
29 {
30     while(n >= 28)
31     {
32         if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
33         {
34             if(n-31>=0) {month++;n -= 31;}
35             else break;
36         } else if(month == 4 || month == 6 || month == 9 || month == 11) {
37             if(n-30>=0) {month++;n -= 30;}
38             else break;
39         } else {
40             if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
41             {
42                 if(n-29>=0) {n -= 29;month++;}
43                 else break;
44             } else {n -= 28;month++;}
45         }
46     }
47 }
48 int main()
49 {
50     while(~scanf("%d",&n) && n != -1)
51     {
52         year = 2000;
53         month = 1;
54         week = n%7;
55         num_year();
56         num_month();
57         printf("%d-%.2d-%.2d %s\n",year,month,n+1,s[week]); // 注意这个n+1,应该是从1月1号开始
58         // 计算,所以要加1,WA了
59     }
60     return 0;
61 }
View Code

 

你可能感兴趣的:(POJ 2080 Calendar (日期类题目))