您的位置:首页 > 编程语言 > C语言/C++

题目1043:Day of Week

2017-08-25 18:52 274 查看
题目描述:

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.
输入:

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.
样例输入:
9 October 2001
14 October 2001

样例输出:
Tuesday
Sunday

提示:

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

C++代码:

#include<iostream>
#include<string>
#include<map>
#include<math.h>
#include<algorithm>

using namespace std;

int ml[14];
int mn[14];

int d,y;
string mon;

bool cmp(int d,int m,int y){
if(y>2017){
return true;
}else if(y<2017){
return false;
}else{
if(m>8){
return true;
}else if(m<7){
return false;
}else{
if(d>=22)
return true;
else
return false;
}

}
return true;
}

int main(){
ml[0]=0;ml[1]=31;ml[2]=29;ml[3]=31;ml[4]=30;ml[5]=31;ml[6]=30;
ml[7]=31;ml[8]=31;ml[9]=30;ml[10]=31;ml[11]=30;ml[12]=31;ml[13]=0;
mn[0]=0;mn[1]=31;mn[2]=28;mn[3]=31;mn[4]=30;mn[5]=31;mn[6]=30;
mn[7]=31;mn[8]=31;mn[9]=30;mn[10]=31;mn[11]=30;mn[12]=31;mn[13]=0;

map<string,int> mymap;
mymap["January"]=1;mymap["February"]=2;mymap["March"]=3;mymap["April"]=4;
mymap["May"]=5;mymap["June"]=6;mymap["July"]=7;mymap["August"]=8;
mymap["September"]=9;mymap["October"]=10;mymap["November"]=11;mymap["December"]=12;

map<int,string> week;
week[0]="Monday";week[1]="Tuesday";week[2]="Wednesday";week[3]="Thursday";
week[4]="Friday";week[5]="Saturday";week[6]="Sunday";

while(cin>>d>>mon>>y){
int m=mymap[mon];
int sum1=0;
int sum2=0;
if(y%4==0&&y%100!=0||y%400==0){
sum1+=ml[m]-d;
for(int i=m+1;i<13;i++){
sum1+=ml[i];
}
sum2=365-sum1;
}else{
sum1+=mn[m]-d;
for(int i=m+1;i<13;i++){
sum1+=mn[i];
}
sum2=364-sum1;
}
int s1=0;
int s2=0;
s1+=mn[8]-21;
for(int i=9;i<13;i++){
s1+=mn[i];
}
s2=364-s1;
int sum=0;
if(y>2017){
sum+=sum2+s1;
int year=2018;
while(year<y){
if(year%4==0&&year%100!=0||year%400==0){
sum+=366;
}else{
sum+=365;
}
year++;
}
}else if(y<2017){
sum+=sum1+s2;
// cout<<sum1<<" "<<s2<<endl;
int year=2016;
while(year>y){
if(year%4==0&&year%100!=0||year%400==0){
sum+=366;
}else{
sum+=365;
}
year--;
}
}else{
int d1=0;
int d2=0;
for(int i=1;i<8;i++){
d1+=mn[i];
}
d1+=21;
for(int i=1;i<m;i++){
d2+=mn[i];
}
d2+=d;
sum=abs(d1-d2)-1;
}
sum+=1;
int w = sum%7;
// cout<<"W="<<w<<endl;
if(cmp(d,m,y)){
cout<<week[w]<<endl;
}
else{
cout<<week[(7-w)%7]<<endl;
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++