问题描述
已知2011年11月11日是星期五,问YYYY年MM月DD日是星期几?注意考虑闰年的情况。尤其是逢百年不闰,逢400年闰的情况。
输入格式
输入只有一行
YYYY MM DD
输出格式
输出只有一行
W
数据规模和约定
1599 <= YYYY <= 2999
1 <= MM <= 12
1 <= DD <= 31,且确保测试样例中YYYY年MM月DD日是一个合理日期
1 <= W <= 7,分别代表周一到周日
样例输入
2011 11 11
样例输出
5
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int week[3005][15][35];
int month1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int month2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
void count(){
int year=2011, month=11 ,day=11, ans=5;
week[year][month][day] = 5;
while(1){
if(year==3000)
break;
if(year % 400 ==0 || (year%4 == 0 && year % 100 != 0)){
day++;
if(day > month2[month])
{
month++;
day=1;
}
if(month > 12)
{
month=1;
year++;
}
ans++;
if(ans > 7)
ans=1;
week[year][month][day]=ans;
}
else {
day++;
if(day > month1[month])
{
month++;
day=1;
}
if(month > 12)
{
month=1;
year++;
}
ans++;
if(ans > 7)
ans=1;
week[year][month][day]=ans;
}
}
year=2011;
month=11;
day=11;
ans=5;
while(1){
if(year == 1598)
break;
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
{
day--;
if(day <= 0){
month--;
if(month <= 0)
day = 31;
else
day = month2[month];
}
if(month <= 0)
{
month=12;
year--;
}
ans--;
if(ans <= 0)
ans=7;
week[year][month][day]=ans;
}
else {
day--;
if(day<=0){
month--;
if(month <= 0)
day=31;
else
day = month1[month];
}
if(month <= 0)
{
month=12;
year--;
}
ans--;
if(ans <= 0)
ans=7;
week[year][month][day]=ans;
}
}
}
int main()
{
count();
int y,m,d;
while(~scanf("%d%d%d",&y,&m,&d))
cout<<week[y][m][d]<<endl;
return 0;
}