http://acm.hdu.edu.cn/showproblem.php?pid=2133
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).
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.
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
时间复杂度:$O(1)$
代码:
#include <bits/stdc++.h>
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(~scanf("%d%d%d", &year, &month, &day)) {
if(IsRunNian(year)) {
if(day > a[month] || month == 0 || day == 0) {
printf("illegal\n");
continue;
}
} else{
if(day > b[month] || month == 0 || day == 0) {
printf("illegal\n");
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;
printf("%s\n",s[sum]);
}
return 0;
}