hdu1308 What Day Is It

题目链接:点击打开链接


本题题目描述略坑 大意是1752.9.2前美国日期用旧算法,9.14后用新算法

另外1.1.1是星期六是什么设定。。算出天数需要减2

旧历日期=year*365+M[month]+day+year/4-2

新历日期=year*365+year/4-year/100+year/400+M[month]+day+1752/100-1752/400-11-2

               =year*365+year/4-year/100+year/400+M[month]+day


February写成Fabruary半天没看出来。。

代码:

#include <iostream>
#include <cstdio>
using namespace std;
int MM[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int M[12]={0,31,59,90,120,151,181,212,243,273,304,334};
char MN[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char DN[7][20]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
bool leapyear(int y){
    if(y<=1752)
        return y%4==0;
    return ((y%4==0)&&(y%100!=0))||y%400==0;
}
bool isvalid(int y,int m,int d){
    if(!m||!d||!y)
        return 0;
    if(m>12)
        return 0;
    if(y==1752&&m==9&&d>2&&d<14)
        return 0;


    if(leapyear(y)){
            if(m==2){
                if(d>29)
                return 0;
            }
            else if(d>MM[m-1])
                return 0;
    }
    else if(d>MM[m-1]){
            return 0;
    }
    return 1;
}








int main()
{
    //freopen("input.txt","r",stdin);
    int y,m,d;
    int totday,curday;
    while(cin>>m>>d>>y,m||d||y){


            if(!isvalid(y,m,d)){
                    printf("%d/%d/%d is an invalid date.\n",m,d,y);
                    continue;
            }


            if(y<1752||(y==1752&&m<9)||(y==1752&&m==9&&d<=2)){
               totday=(y-1)*365+(y-1)/4;//-(y-1)/100+(y-1)/400;
               totday+=M[m-1];
               totday+=d;
               if(y%4==0&&m>2){
                totday++;
               }
               totday-=2;
               curday=totday%7;
            }
            else{
               totday=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400;
               totday+=M[m
               -1];
               totday+=d;
               if(leapyear(y)&&m>2){
                totday++;
               }
               curday=totday%7;
            }
            if(curday==0)
                curday=7;
        //    cout<<curday<<endl;
            printf("%s %d, %d is a %s\n",MN[m-1],d,y,DN[curday-1]);
    }
    return  0;
}




你可能感兴趣的:(水题)