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
Source: Asia 2004, Shanghai (Mainland China), Preliminary
分析:
题意:
给出从公元2000年1月1日起消逝的天数,要求输出这天的日期(年,月,日)和星期。年份不会超过9999。有多组测试数据。
基本不需要什么算法。主要是编程比较麻烦,要考虑所有情况。一种比较好的想法是:先打表,把年历存储起来(包括所需的全部信息),再根据输入直接查找。因为vector是随机查找,是O(1)的时间复杂度。
另外就是格式上稍微注意一下。
还是比较简单的一个题,就是要有耐心。
ac代码:
#include <iostream>
#include<cstdio>
#include<vector>
using namespace std;
struct Info//用结构体存储
{
short year,month,day,week;
};
vector<Info> v;//放进vector容器
bool RR(int year)//判断闰年
{
if(year%4==0&&year%100!=0||year%400==0)
return true;
else return false;
}
void table()//打表
{
int i,j,k;
Info info;
bool flag=false;
short week=5;//2000年一月一号是周六,前一天是周五
for(i=2000;i<=9999;i++)//年
{
flag=RR(i);//先判断是否是闰年
for(j=1;j<=12;j++)//月
{
for(k=1;k<=31;k++)//日
{
if(j==4||j==6||j==9||j==11)
{
if(k==31) break;
}
else if(j==2)
{
if(flag)
{
if(k>=30) break;
}
else
{
if(k>=29) break;
}
}
info.year=i;
info.month=j;
info.day=k;
week++;
if(week>7) week=1;
info.week=week;
v.push_back(info);
}
}
}
}
int main()
{
int n;
table();
while(scanf("%d",&n)&&n!=-1)
{
printf("%d-",v[n].year);
if(v[n].month<10)
printf("0");
printf("%d-",v[n].month);
if(v[n].day<10)
printf("0");
printf("%d ",v[n].day);
if(v[n].week==1)
printf("Monday\n");
else if(v[n].week==2)
printf("Tuesday\n");
else if(v[n].week==3)
printf("Wednesday\n");
else if(v[n].week==4)
printf("Thursday\n");
else if(v[n].week==5)
printf("Friday\n");
else if(v[n].week==6)
printf("Saturday\n");
else if(v[n].week==7)
printf("Sunday\n");
}
return 0;
}