poj2080Calendar(历法)

xiaoxiao2021-02-28  142

Calendar Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13787 Accepted: 4888 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 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

仔细模拟进行处理

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int is_leep(int x)///闰年 { if((x%4==0&&x%100!=0)||(x%400==0)) return 366; return 365; } int is_MM(int MM,int YY)///1代表大月,0代表小月 2000 1 1 周六 { int ans=0; if(MM==2) return is_leep(YY)==366?29:28; switch(MM) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: ans=31; break; default: ans=30; } return ans; } string is_week(int x) { string str; if((x-2)%7==1) { str="Monday"; return str; } else if((x-2)%7==2) { str="Tuesday"; return str; } else if((x-2)%7==3) { str="Wednesday"; return str; } else if((x-2)%7==4) { str="Thursday"; return str; } else if((x-2)%7==5) { str="Friday"; return str; } else if((x-2)%7==6) { str="Saturday"; return str; } else if((x-2)%7==0) { str="Sunday"; return str; } } int main() { int Y,M,D; int num; while(scanf("%d",&num)!=EOF&&num!=-1) { int k=num; Y=2000,M=1,D=1; while(num) { if(num>=is_leep(Y)) { num-=is_leep(Y); Y++; } else if(num>=is_MM(M,Y)) { num-=is_MM(M,Y); M++; } else { D+=num; num=0; } } string s; s=is_week(k+1); printf("%d-d-d ",Y,M,D); cout<<s<<endl; //cout<<Y<<'-'<<M<<'-'<<D+1<<' '<<s<<endl; //cout<< } return 0; }
转载请注明原文地址: https://www.6miu.com/read-82655.html

最新回复(0)