alendar
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
Source
题目类型:模拟
算法分析:直接进行日期模拟即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <cstdio> #include <iostream> #include <fstream> using namespace std; int dayofyears (int year) { if (year % 4 == 0 && year % 100 != 0) return 366; if (year % 400 == 0) return 366; else return 365; } int dayofmonths (int month, int year) { if (month == 2) { if (dayofyears (year) == 366) return 29; else return 28; } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) return 31; else return 30; } int main() { int N, year, month, day, temp; char week[][20] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; //fstream cin ("aaa.txt"); cin >> N; while (N != -1) { year = 2000, month = 1, day = 1; temp = N; while (N >= dayofyears (year)) { N -= dayofyears (year); year++; } while (N >= dayofmonths (month, year)) { N -= dayofmonths (month, year); month ++; } N++; if (month >= 10) { if (N < 10) printf ("%d-%d-0%d %s\n", year, month, N, week[temp%7]); else printf ("%d-%d-%d %s\n", year, month, N, week[temp%7]); } else { if (N < 10) printf ("%d-0%d-0%d %s\n", year, month, N, week[temp%7]); else printf ("%d-0%d-%d %s\n", year, month, N, week[temp%7]); } cin >> N; } return 0; } |
- « 上一篇:poj2031
- poj2082:下一篇 »