描述
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
#include <iostream>
#include <string>
#include <map>
using namespace std;
//给日期 算那天为星期几 以现在的时间为基准
//分类讨论 2024/1/22 之前和之后
//今天-future (累积天数+nweekday)%7=fweekday
//past-今天 (nweekday + 累积天数)%7=fweekday; >== pweekday = ((7+nweekday)- totalDay % 7) % 7;
int main() {
//int->string
string intToweekday[8] = { "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int mday[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//将英文星期转为数字类型的星期 string->int 用map
map<string,int> monToint={{"January", 1},
{"February", 2},
{"March", 3},
{"April", 4},
{"May", 5},
{"June", 6},
{"July", 7},
{"August", 8},
{"September", 9},
{"October", 10},
{"November", 11},
{"December", 12}
};
int day,mon,year;
string month;
bool isbefore;
while(cin>>day>>month>>year){
mon=monToint[month];//从字符到整数
//分类讨论 before or future
if(year<2024||year==2024&&mon<1||year==2024&&mon==1&&day<22){
isbefore=true;
} else{
isbefore= false;
}
//算总数 从begin到end
int begYear,begMon,begDay,endYear,endMon,endDay;
if(isbefore){
begYear=year;
begMon=mon;
begDay=day;
endYear=2024;
endMon=1;
endDay=22;
} else{
begYear=2024;
begMon=1;
begDay=22;
endYear=year;
endMon=mon;
endDay=day;
}
int totalday=0;
while (true){
if(begYear==endYear&&begMon==endMon&&begDay==endDay){
break;
}
totalday++;
//闰年判断
bool isLeap = begYear % 400 == 0 || begYear % 4 == 0 && begYear % 100 != 0;
if (isLeap) {
mday[2] = 29;
}
else {
mday[2] = 28;
}
begDay++;
if(begDay>mday[begMon]){
begDay=1;
++begMon;
if(begMon>12){
begMon=1;
++begYear;
}
}
}
if (isbefore) {
//(x + totalDay)%7=1; >== x = (8- totalDay % 7) % 7;
cout<< intToweekday[(8 - totalday % 7) % 7]<<endl;
}
else {
cout<<intToweekday[(totalday + 1) % 7]<<endl;
}
}
return 0;
}