Calendar
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11755 | Accepted: 4325 |
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
Source
题意:初始日期是2000.1.1日,星期六,然后给你一个n代表距离初始日期多少天,让你求n天之后是几几年几月几日,星期几。
做法:先设y代表年,m代表月,d代表日,w代表星期,并分别初始化为2000,1,1,6,然后用一个while语句,对n进行减操作,while中首先判断y是否是闰年,如是则把第二月的日子改为29,不是则不变,然后判断n是否大于y这年的总天数,如是则n-=y年的天数,如不是则从1-12循环,减到n为0为止。最后还要注意d不能超过m月份的总天数,m不能大于12。星期的话直接n%7来进行判断就行。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
int mm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char wk[8][20]={" ","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday","Sunday"};
int main()
{
int n;
int i,j,flag;
int y,m,d,w;
while(scanf("%d",&n)&&n!=-1)
{
y=2000;
m=1;
d=1;
w=(6+n%7)%7;
if(w==0)
w=7;
while(n!=0)
{
flag=0;
if((y%100!=0&&y%4==0)||(y%100==0&&y%400==0))
{
flag=1;
}
if(flag==1)
{
mm[2]=29;
if(n>=366)
{
n-=366;
y++;
}
else
{
for(i=1;i<=12;i++)
{
if(n>=mm[i])
{
n-=mm[i];
m++;
}
else
{
d+=n;
n=0;
break;
}
}
}
}
if(flag==0)
{
mm[2]=28;
if(n>=365)
{
n-=365;
y++;
}
else
{
for(i=1;i<=12;i++)
{
if(n>=mm[i])
{
n-=mm[i];
m++;
}
else
{
d+=n;
n=0;
break;
}
}
}
}
}
if(d>mm[m])
{
d-=mm[m];
m++;
}
if(m>12)
{
y++;
m-=12;
}
printf("%d-%02d-%02d %s\n",y,m,d,wk[w]);
}
}