首页 > ACM题库 > HDU-杭电 > hdu 2133 What day is it[解题报告]C++
2013
12-29

hdu 2133 What day is it[解题报告]C++

What day is it

问题描述 :

Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?

输入:

There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

输出:

There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).

样例输入:

2007 11 17

样例输出:

Saturday

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2133

题意:输入一个日期,输出不合法(illegal)或星期几。

mark:直接计算该日期和0年1月1日(周六)天数差,然后模7就好,注意闰年还有合法性判断要包括0月和0日的情况。

代码:

# include <stdio.h>


int mon[2][13] = {    0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
                    0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;


int isleap(int y){return (y%4==0&&y%100!=0)||(y%400==0);}


int illegal(int y, int m, int d)
{
    if (m*d == 0) return 1 ;
    if (d > mon[isleap(y)][m]) return 1 ;
    return 0 ;
}


int thisyear(int y, int m, int d)
{
    int i, rtn = 0 ;
    for (i = 0 ; i < m ; i++)
        rtn += mon[isleap(y)][i] ;
    return (rtn + d) % 7 ;
}


int calc(int y, int m, int d)
{
    int i, rtn = 0 ;
    for(i=0;i<y;i++)
        rtn += isleap(i)?366:365 ;
    return (rtn + thisyear(y,m,d)+4) % 7 ;
}


int main ()
{
    int y, m, d ;
    char tab[7][10] = {    "Monday",
                        "Tuesday",
                        "Wednesday",
                        "Thursday",
                        "Friday",
                        "Saturday",
                        "Sunday"} ;
    while (~scanf ("%d %d %d%*c", &y, &m, &d))
    {
        if (illegal(y,m,d)) puts ("illegal") ;
        else puts (tab[calc(y,m,d)]) ;
    }
    return 0 ;
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/22/2328732.html