http://poj.org/problem?id=2080
Calendar
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11281 | Accepted: 4168 |
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 -1Sample Output
2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday译文:问题描述: 在我们现在使用的日历中, 闰年被定义为能被4整除的年份 但是能被100整除而不能被400整除的年是例外,它们不是闰年。例如:1700, 1800, 1900 和 2100 不是闰年,而 1600, 2000 和 2400是闰年。 给定从公元2000年1月1日(周六)开始逝去的天数,你的任务是给出这一天是哪年哪月哪日星期几。输入: 输入包含若干行,每行包含一个正整数,表示从2000年1月1日开始逝去的天数。输入最后一行是−1, 不必处理。可以假设结果的年份不会超过9999。 输出: 对每个测试样例,输出一行,该行包含对应的日期和星期几。 格式为“YYYY-MM-DD DayOfWeek”, 其中 “DayOfWeek” 必须是下面中的一个: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday“。代码下边附测试数据,通过这些基本就没问题,应该就可以AC#include<stdio.h> #include<string.h> int judge(int Y) { if(Y%400==0||Y%4==0&&Y%100!=0) return 1; else return 0; } int main() { int N,Y,M,W,i,j,k,a[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; char s[10][20],b[20]; strcpy(s[1],"Saturday");strcpy(s[2],"Sunday");strcpy(s[3],"Monday");strcpy(s[4],"Tuesday"); strcpy(s[5],"Wednesday");strcpy(s[6],"Thursday");strcpy(s[0],"Friday"); while(scanf("%d",&N)&&N!=-1) { memset(b,'\0',sizeof(b)); W=(N+1)%7;//过完N天是星期s[w] strcpy(b,s[W]); int sum=0,s=0; for(Y=2000;;Y++)//这年是Y { if(judge(Y)==0) s=365; if(judge(Y)==1) s=366; sum+=s; if(sum>=N+1)//注意这里有等号,这里当到了年末的最后一天也跳出循环,不跳出的话直接到下一年了,直接忽略这一天 break; } sum-=s; for(M=1;M<=12;M++) { if(M==2&&judge(Y)==1) s=a[M]+1; else s=a[M]; sum+=s; if(sum>=N+1)//注意这里有等号 ,这里当月末的最后一天跳出,不跳出直接略过这一天,到下一个月。 break; } sum-=s; printf("%d-%.2d-%.2d %s\n",Y,M,N-sum+1,b); } return 0; } //0 //1 //364 //365 //366 //367