2964:日历问题-poj
2964:日历问题
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 在我们现在使用的日历中, 闰年被定义为能被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" 或 "Saturday“。
- 样例输入
-
1730 1740 1750 1751 -1
- 样例输出
-
2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday
- 提示
- 2000.1.1. 是星期六
- 这题目有好几种思路可解决,只是我从天数开始递减,得到年月日,结果能通过一部分,没找到问题出在哪了。
- 贴了两个代码。还比较好理解:
- 代码:
- 第一个代码是我自己写的,之前提交了一个没通过,还以为这题有毒,还有,我把之前的月份计算改为下标递增求解,测试数据结果都是一样的,但都是AC 不了,也没找出问题。
-
#include<iostream> #include<stdio.h> using namespace std; int days_of_year[2]={365,366}; int days_of_month[24]={31,28,31,30,31,30,31,31,30,31,30,31,/**/31,29,31,30,31,30,31,31,30,31,30,31}; char week[7][20]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; int isleapyear(int year) { if(year%400==0||(year%4==0&&year%100!=0)) return 1; else return 0; } int main() { int m_days;//距离天数 int weekday;//定义标记当前是星期几 while(true) { scanf("%d",&m_days); weekday=m_days%7; int start_year=2000,start_month=1,start_day=1;//定义当前的年月日,随着天数的增加,依次递增,最后达到所要的年份月份日期 if(m_days==-1) { break; } weekday=m_days%7; while(m_days>=days_of_year[isleapyear(start_year)])//计算年 { m_days-=days_of_year[isleapyear(start_year)]; start_year++; } while(m_days>=days_of_month[isleapyear(start_year)*12+start_month-1])//计算月 { m_days-=days_of_month[isleapyear(start_year)*12+start_month-1]; start_month++; } start_day=start_day+m_days;//计算天,这个是年月计算后剩下的天数 printf("%d-%02d-%02d %s\n",start_year,start_month,start_day,week[weekday]); } return 0; }
-
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; int days_of_year[2] = {365,366}; int days_of_month[24] = {31,28,31,30,31,30,31,31,30,31,30,31,/**/31,29,31,30,31,30,31,31,30,31,30,31}; char days_of_week[7][20] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" ,"Saturday"}; int main() { int day; int start_year,start_month,start_day,start_week; int is_leap_year; while(scanf("%d",&day),day!=-1) { start_year = 2000; start_month = 1; start_day = 1; start_week = 6; start_week = (start_week + day) % 7; //判断是否闰年 if(start_year % 4 == 0 && !(start_year % 100 == 0 && start_year % 400 != 0)) { is_leap_year = 1; } else { is_leap_year = 0; } while(day >= days_of_year[is_leap_year]) { start_year ++; day -= days_of_year[is_leap_year]; //判断是否闰年 if(start_year % 4 == 0 && !(start_year % 100 == 0 && start_year % 400 != 0)) { is_leap_year = 1; } else { is_leap_year = 0; } } while(day >= days_of_month[is_leap_year*12 + start_month - 1]) { day -= days_of_month[is_leap_year*12 + start_month - 1]; start_month ++; } start_day += day; printf("%d-%02d-%02d %s\n",start_year,start_month,start_day,days_of_week[start_week]); } return 0; }
代码二:
#include <stdio.h> int judgeyear(int); int main() { long days; int i,j; int mon[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}}; int year[2]={365,366}; char day[7][10]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; int dow; while(scanf("%ld",&days),days!=-1) { dow=days%7; days+=1; for(i=2000 ; (days-year[judgeyear(i)])>0 ; i++) days-=year[judgeyear(i)]; int t=judgeyear(i); for(j=1 ; (days-mon[t][j])>0 ; j++) days-=mon[t][j]; printf("%d-%02d-%02ld %s\n",i,j,days,day[dow]); } return 0; } int judgeyear(int a) { if(a%4!=0 || (a%100 == 0 && a%400 !=0)) return 0; else return 1; }
以大多数人努力程度之低,根本轮不到去拼天赋~