首页 > 代码库 > HDU2133 What day is it【水题】

HDU2133 What day is it【水题】

What day is it

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3976    Accepted Submission(s): 1164

Problem Description
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?
 
Input
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).
 
Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
 
Sample Input
2007 11 17
 
Sample Output
Saturday
 
Author
LGX
 
Source

HDU 2007-11 Programming Contest_WarmUp


题目大意:给你一个日期,先判断日期是否合法,不合法输出"illegal",继续下组数据输入。

法的话,计算出该日期是周几,然后输出相应星期的英文单词。参考日期是2007年11月17日是

星期六。

思路:按2007年11月17日来算太过繁琐,因为公元0001年1月1日是星期一,这样递推算到要计

算的日期就变简单了不少。统计从1年1月1日到给出日期的总天数是多少,因为只需要计算星期,

所以只要对7取余的值即可。用了数组来保存每月天数和星期的单词,简洁了不少。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int a[13] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int b[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
char s[8][10] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
bool IsRunNian(int year)
{
    if((year%4==0 && year%100!=0) || year%400==0)
        return true;
    return false;
}
int main()
{
    int year,month,day;
    while(cin >> year >> month >> day)
    {
        if(IsRunNian(year))
        {
            if(day > a[month] || month == 0 || day == 0)
            {
                cout << "illegal" << endl;
                continue;
            }
        }
        else
        {
            if(day > b[month] || month == 0 || day == 0)
            {
                cout << "illegal" << endl;
                continue;
            }
        }
        int sum = 0;
        for(int i = 1; i < year; ++i)
        {
            if(IsRunNian(i))
                sum += 366;
            else
                sum += 365;
            sum %= 7;
        }
        for(int i = 0; i < month; ++i)
        {
            if(IsRunNian(year))
                sum += a[i];
            else
                sum += b[i];
            sum %= 7;
        }

        sum += day;
        sum %= 7;
        cout << s[sum] << endl;
    }

    return 0;
}


HDU2133 What day is it【水题】