/*Calendar
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11251 Accepted: 4160
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.
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 <span style="color:#ff0000;">date won’t be after the year 9999.
</span>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
Shanghai 2004 Preliminary*/
#include<stdio.h>
#include<string.h>
int leap(int x)
{
if( (x%400 == 0) || ( x % 100 != 0 && x % 4 == 0))
return 1;
return 0;
}
int main()
{
int a[9000], i, j = 0, n, b[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
for(i = 2000; i < 10010; i++)
if(leap(i))
a[j++] = 366;
else
a[j++] = 365;
int year, mon, day;
char week[10];
while(scanf("%d", &n) != EOF)
{
if(n == -1)
break;
year = 2000;
mon = 1;
switch ((n-1) % 7)
{
case 0:strcpy(week, "Sunday");break;
case 1:strcpy(week, "Monday");break;
case 2:strcpy(week, "Tuesday");break;
case 3:strcpy(week, "Wednesday");break;
case 4:strcpy(week, "Thursday");break;
case 5:strcpy(week, "Friday");break;
case 6:strcpy(week, "Saturday");break;
}
for(i = 0; i < 9000; i++)
if(n >= a[i])//大于等于
{
n -= a[i];
year++;
}
else
break;
for(i = 0; i < 12; i++)
{
if(i == 1 )
{
if(leap(year))
{
if(n >= b[i]+1)//大于等于
{
n -= (b[i]+1);
mon++;
}
else
break;
}
else
{
if(n >= b[i])//大于等于
{
n -= b[i];
mon++;
}
else
break;
}
}
else
{
if(n >= b[i])//大于等于
{
n -= b[i];
mon++;
}
else
break;
}
}
day = n+1;
if(mon > 9 && day > 9)
printf("%d-%d-%d %s\n", year, mon, day, week);
else if( mon > 9 && day < 10)
printf("%d-%d-0%d %s\n", year, mon, day, week);
else if(mon < 10 && day > 9)
printf("%d-0%d-%d %s\n", year, mon, day, week);
else
printf("%d-0%d-0%d %s\n", year, mon, day, week);
}
return 0;
}
从2000-01-01开始,给定一个天数,求出年月日和星期几。
难点在于边界判断,年和月的边界要大于等于才行,不然一直wa。
题目还要求年数不会超过9999.......