2964:日历问题-poj
Posted A-inspire
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. 是星期六
- 这题目有好几种思路可解决,只是我从天数开始递减,得到年月日,结果能通过一部分,没找到问题出在哪了。
- 贴了两个代码。还比较好理解:
- 代码:
-
#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; }