1929: Day of Week
时间限制: 1 Sec 内存限制: 32 MB献花: 339 解决: 104
[ 献花][ 花圈][ TK题库]
题目描述
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define inf 0x3f3f3f
int month[13][2]= {{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
//month数组用来表示平年和闰年的每个月的天数,第一维表示月份,第二维0表示平年,1表示闰年
string monthname[13]={
"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
string weekday[7]={
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"};
string ouweekday[7] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
bool judge(int y)//判断平闰年
{
return (y%4==0&&y%100!=0)||(y%400==0);
}
int main()
{
int a,b;
string s;
int m;
while(cin>>a>>s>>b)
{
for(int i=1;i<=12;i++)
{
if(s==monthname[i])
{
m=i;
break;
}
}
int d=a+m*100+b*10000;
int date1=d<20180422?d:20180422;
int date2=d>20180422?d:20180422;
int y1=date1/10000,m1=date1%10000/100,d1=date1%100;
int y2=date2/10000,m2=date2%10000/100,d2=date2%100;
int ans=0;
while(y1<y2||m1<m2||d1<d2)//两个日期不相等就进行加一运算
{
d1++;
if(d1>month[m1][judge(y1)])//如果小日期的天数大于该月的天数,进入下一个月,月份加一,置为下月1号
{
m1++;
d1=1;
}
if(m1==13)//如果月份大于12,则进入下一年,月份置一
{
y1++;
m1=1;
}
ans++;//累计天数
}
//cout<<ans<<endl;
int cha=ans%7;
if(d>20180422)//当题目所给时间比自己选定标准时间大时
{
cout<<weekday[cha]<<endl;
}
else//题目所给时间比自己选定时间小
{
if(cha==0)
cout<<"Sunday"<<endl;
else
cout<<ouweekday[6-cha]<<endl;
}
}
return 0;
}
输入
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
输出
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday