zoj 1256

What Day Is It?

Time Limit: 2 Seconds       Memory Limit: 65536 KB

The calendar now in use evolved from the Romans. Julius Caesar codified a calendar system that came to be known as the Julian calendar. In this system, all months have 31 days, except for April, June, September, and November, which have 30 days, and February, which has 28 days in non-leap years, and 29 days in leap years. Also, in this system, leap years happened every four years. That is because the astronomers of ancient Rome computed the year to be 365.25 days long, so that after every four years, one needed to add an extra day to keep the calendar on track with the seasons. To do this, they added an extra day (February 29) to every year that was a multiple of four.

Julian Rule: 

Every year that is a multiple of 4 is a leap year, i.e. has an extra day (February 29).

In 1582, Pope Gregory's astronomers noticed that the year was not 365.25 days long, but closer to 365.2425. Therefore, the leap year rule would be revised to the following:

Gregorian Rule: 

Every year that is a multiple of 4 is a leap year, unless it is a multiple of 100 that is not a multiple of 400.

To compensate for how the seasons had shifted against the calendar up until that time, the calendar was actually shifted 10 days: the day following October 4, 1582 was declared to be October 15.

England and its empire (including the United States) didn't switch to the Gregorian calendar system until 1752, when the day following September 2 was declared to be September 14. (The delay was caused by the poor relationship between Henry VIII and the Pope.)

Write a program that converts dates in the United States using a calendar of the time and outputs weekdays.


Input 

The input will be a series of positive integers greater than zero, three integers per line, which represent dates, one date per line. The format for a date is ``month day year" where month is a number between 1 (which indicates January) and 12 (which indicates December), day is a number between 1 and 31, and year is positive number.


Output 

The output will be the input date and name of the weekday on which the given date falls in the format shown in the sample. An invalid date or nonexistent date for the calendar used in the United States at the time should generate an error message indicating a invalid date. The input will end with three zeroes.


Sample Input

11 15 1997
1 1 2000
7 4 1998
2 11 1732
9 2 1752
9 14 1752
4 33 1997
0 0 0


Sample Output

November 15, 1997 is a Saturday
January 1, 2000 is a Saturday
July 4, 1998 is a Saturday
February 11, 1732 is a Friday
September 2, 1752 is a Wednesday 
September 14, 1752 is a Thursday
4/33/1997 is an invalid date.


Source:  Pacific Northwest 1997



题意:以1752.9.2为界,在这之前是以前的公历,年份能被4整除就是闰年,在这之后是新的公历,年份是不能被100整除但能被4整除或者能被400整除才是闰年,然后现在让你求的是给你一个日子,让你求出那天是星期几。

做法:首先判断是不是正常有效的日子,判断的条件是月份在1-12,天数<=月份的天数,同时要注意1752.9.3-9.13是不合法的,然后算出当前年份到公元0年的总天数,一年一年的判断下去,记总天数为ans,最后在判断星期几的时候要判断当前的年份是否是旧的公历,如果是的话星期就是(ans+12)%7,否则就是(ans+1)%7。


#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char m1[][20]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
char w1[][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int old(int mm,int dd,int yy)
{
    if(yy<1752)
        return 1;
    if(yy==1752&&mm<9)
        return 1;
    if(yy==1752&&mm==9&&dd<=2)
        return 1;
    return 0;
}
int pd(int mm,int dd,int yy)
{
    if(yy==1752&&mm==9)
        if(dd>2&&dd<14)
        return 0;
    if(mm>12||mm<1||dd<1)
        return 0;
    if(old(mm,dd,yy)==1)
    {
        if(yy%4==0)
            m[2]=29;
        else
            m[2]=28;
    }
    else
    {
        if((yy%100!=0&&yy%4==0)||(yy%100==0&&yy%400==0))
            m[2]=29;
        else
            m[2]=28;
    }
    if(dd>m[mm])
        return 0;
    return 1;
}
int main()
{
    int M,D,Y,Y1;
    int i,j,ans,flag;
    while(scanf("%d%d%d",&M,&D,&Y)&&M&&D&&Y)
    {
        Y1=Y;
        flag=pd(M,D,Y);
        if(flag==0)
        {
            printf("%d/%d/%d is an invalid date.\n",M,D,Y);
            continue;
        }
        ans=0;
        m[2]=28;
        if(old(M,D,Y))
        {
            if(Y%4==0)
                m[2]=29;
        }
        else
        {
            if((Y%100!=0&&Y%4==0)||(Y%100==0&&Y%400==0))
                m[2]=29;
        }
        for(i=1;i<M;i++)
            ans+=m[i];
        ans+=D;
        for(Y=1;Y<Y1;Y++)
        {
            if(Y<1752)
            {
                if(Y%4==0)
                    ans+=366;
                else
                    ans+=365;
            }
            else
            {
                if((Y%100!=0&&Y%4==0)||(Y%100==0&&Y%400==0))
                    ans+=366;
                else
                    ans+=365;
            }
        }
        if(old(M,D,Y))
        {
            printf("%s %d, %d is a %s\n",m1[M],D,Y1,w1[(ans+12)%7]);
        }
        else
        {
            printf("%s %d, %d is a %s\n",m1[M],D,Y1,w1[(ans+1)%7]);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值