Bessie asked her friend what day of the week she was born on. She knew that she was born on 2003 May 25, but didn't know what day it was. Write a program to help. Note that no cow was born earlier than the year 1800.
Facts to know:
* January 1, 1900 was on a Monday.
* Lengths of months:
* Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year).
* The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Facts to know:
* January 1, 1900 was on a Monday.
* Lengths of months:
Jan 31 May 31 Sep 30 Feb 28 or 29 Jun 30 Oct 31 Mar 31 Jul 31 Nov 30 Apr 30 Aug 31 Dec 31
* Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year).
* The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
* Line 1: Three space-separated integers that represent respectively the year, month (range 1..12), and day of a date.
* Line 1: A single word that is the day of the week of the specified date (from the lower-case list: monday, tuesday, wednesday, thursday, friday, saturday, sunday).
2003 5 25
sunday
INPUT DETAILS:
May 25, 2003
OUTPUT DETAILS:
May 25, 2003
OUTPUT DETAILS:
May 2003 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
算出两个时间相距的天数,在对7求余算星期,注意这里以1900年1月1日为判断点,有的时间大于它,有的时间小于它要分开求
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
int year,month,day;
bool isleapyear(int y)
{
if(y%100!=0&&y%4==0) return true;
if(y%100==0&&y%400==0) return true;
return false;
}
int howmanyday(int y,int m)
{
if(m==2) {if(isleapyear(y)) return 29; return 28;}
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) return 31;
else return 30;
}
int main()
{
int i;
scanf("%d%d%d",&year,&month,&day);
if(year<1900)
{
int sum=0;
for(i=1899;i>year;i--)
{
if(isleapyear(i)) sum+=366;
else sum+=365;
}
for(i=12;i>month;i--)
{
sum+=howmanyday(year,i);
}
sum+=howmanyday(year,month)-day+1;
switch(sum%7)
{
case 0: printf("monday\n");break;
case 1: printf("sunday\n");break;
case 2: printf("saturday\n");break;
case 3: printf("friday\n");break;
case 4: printf("thursday\n");break;
case 5: printf("wednesday\n");break;
case 6: printf("tuesday\n");break;
}
}
else
{
int sum=0;
for(i=1900;i<year;i++)
{
if(isleapyear(i)) sum+=366;
else sum+=365;
}
for(i=1;i<month;i++)
{
sum+=howmanyday(year,i);
}
sum+=day-1;
switch(sum%7)
{
case 0: printf("monday\n");break;
case 6: printf("sunday\n");break;
case 5: printf("saturday\n");break;
case 4: printf("friday\n");break;
case 3: printf("thursday\n");break;
case 2: printf("wednesday\n");break;
case 1: printf("tuesday\n");break;
}
}
return 0;
}