Day of week
刷题链接: link.
题目描述
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 2005, 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.
输入描述:
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.
Month and Week name in Input/Output:
January, February, March, April, May, June, July, August, September, October, November, December,Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday### 示例1:
输入:
9 October 2001
14 October 2001
输出:
Tuesday
Sunday
题意:给你多个月份为英文的日期,分别判断是星期几,并用英文输出。
思路:判断该日期距离1年1月1日的天数,根据天数判断是星期几。
我的代码:(注意可以使用map表示中英文月份之间的关系)
#include<cstdio>
#include<string>
#include<map>
#include<iostream>
using namespace std;
int judge(int y)
{
if((y%400==0)||(y%100!=0&&y%4==0))
return 1;
return 0;
}
int main()
{
map<string,int>e_month={{"January",1},{"February",2},{"March",3},{"April",4},{"May",5},
{"June",6},{"July",7},{"August",8},{"September",9},{"October",10},{"November",11},{"December",12}};
string e_week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
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};
int d1=1,d2,y1=1,y2,m1=1,m2;
string mon;
while(cin>>d2>>mon>>y2)
{
m2=e_month[mon];
int count=1;
while((d1!=d2)||(m1!=m2)||(y1!=y2))
{
d1++;
if(d1>month[m1][judge(y1)])
{
d1=1;m1++;
}
if(m1>12)
{
m1=1;y1++;
}
count++;
}
cout<<e_week[count%7]<<endl;
}
return 0;
}