POJ-3047

来源:互联网 发布:剑网三炮哥新手数据 编辑:程序博客网 时间:2024/06/11 08:53

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: 
    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.
Input
* Line 1: Three space-separated integers that represent respectively the year, month (range 1..12), and day of a date.
Output
* 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).
Sample Input
2003 5 25
Sample Output
sunday
Hint
INPUT DETAILS: 
May 25, 2003 

OUTPUT DETAILS: 
      May 2003Su Mo Tu We Th Fr Sa             1  2  3 4  5  6  7  8  9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31

    一个简单的判断某年某月某日是周几的题,是从1800年开始的,注意不是1900年。算一下1800.1.1到要求的那天总天数,对7求余数,根据1800.1.1是周几判断就好了。至于1800.1.1是怎么来的,可以根据1900.1.1是sunday算,我偷了个懒,百度万年历哈哈。

代码如下:

#include<iostream>#include<cstring>using namespace std;int main(){    int year,month,day;    int mot[12]={31,28,31,30,31,30,31,31,30,31,30,31};    while(cin>>year>>month>>day)    {        int sum=0;        for(int i=1800;i<year;i++)        {            sum+=365;            if((i%4==0 && i%100!=0) || i%400==0)            {                sum+=1;            }        }        for(int j=1;j<month;j++)        {            sum+=mot[j-1];        }        if(month>2)        {            if((year%4==0 && year%100!=0) || year%400==0)            {                sum++;            }        }        sum+=day;        sum--;        if(sum%7==5)            cout<<"monday"<<endl;        if(sum%7==6)            cout<<"tuesday"<<endl;        if(sum%7==0)            cout<<"wednesday"<<endl;        if(sum%7==1)            cout<<"thursday"<<endl;        if(sum%7==2)            cout<<"friday"<<endl;        if(sum%7==3)            cout<<"saturday"<<endl;        if(sum%7==4)            cout<<"sunday"<<endl;    }    return 0;}