Calendar
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 12842 | Accepted: 4641 |
Description
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.
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.
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
注意:选取 Day of Week is Sunday 的日期作为基准日期.
#include <iostream> using namespace std; int mds[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; char Week[8][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; bool isLeap(int year) { if((year%4==0&&year%100!=0)||year%400==0) { return true; } return false; } int main() { int standard=0; for(int i=2000;i<=2015;i++) { if(isLeap(i)) standard+=366; else standard+=365; } for(int i=1;i<=6;i++) { standard+=mds[i]; } if(isLeap(2016)) standard++; standard+=3;//2016-07-03 is Sunday. int diff; int days; while(cin>>days&&days!=-1) { diff=days-standard; diff++; int year=2000; int d=1; int m=1; while(true) { if(isLeap(year)) { if(days>=366) { days-=366; year++; } else break; } else { if(days>=365) { days-=365; year++; } else break; } } while(true) { int ms=mds[m]; if(isLeap(year)&&m==2) { ms++; } if(days>=ms) { days-=ms; m++; } else break; } d+=days; cout<<year<<"-"; if(m<10) cout<<"0"; cout<<m<<"-"; if(d<10) cout<<"0"; cout<<d<<" "; if(diff>=0) { diff%=7; cout<<Week[diff]<<endl; } else { diff=-diff; diff%=7; if(diff==0) diff=7; cout<<Week[7-diff]<<endl; } } return 0; }