题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2133
What day is it
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3755 Accepted Submission(s): 1096
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
以1年1月1日是星期一为标准,算出1年1月1日到今天的天数,最后让天数对7取模即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=1005,maxe=100005,inf=1<<29;
int n,m;
int v[2][12]={
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
char ans[7][10] = {"Monday","Tuesday", "Wednesday",
"Thursday", "Friday","Saturday", "Sunday"} ;
int judge(int n)
{
if(n%400==0||(n%100!=0&&n%4==0)) return 1;
return 0;
}
int main()
{
int year,month,day;
while(~scanf("%d%d%d",&year,&month,&day))
{
int sum=0;
int f=judge(year);
if(month*day==0||v[f][month-1]<day) {printf("illegal\n");continue;}//不合法
for(int i=1;i<year;i++) sum+=(judge(i)?366:365);//算出前n-1年的天数
for(int i=0;i<month-1;i++) sum+=v[f][i];
sum+=day;//再加上到这一天所经历的天数
printf("%s\n",ans[(sum-1)%7]);
}
return 0;
}