题目描述
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.
样例输入
21 December 2012
5 January 2013
样例输出
Friday
Saturday
思路:从1年1月1日(星期一)计算到今年(输入年份)的天数差值,然后再用问题A的方法计算到今日的天数差值,最后对7取余,所得的数就是星期几。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int year[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
string month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
string week[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int main()
{
int y1,y2,m1,m2,d1,d2,flag,i,j,day;
string mstr;
while(cin>>d2>>mstr>>y2){
for(i=0;i<12;i++)
if(mstr==month[i])
break;
y1=1,m1=1,d1=1;
m2=i+1;
day=0;
for(y1=1;y1<y2;y1++){ //从1.1.1开始计算直到输入的年份的去年的天数
if(y1%4==0&&y1%100!=0||y1%400==0)
day+=366;
else
day+=365;
}
day++; //输入的年份的1月1日
while(!(y1==y2&&m1==m2&&d1==d2)){
flag=0;
if(y1%4==0&&y1%100!=0||y1%400==0)
flag=1; //闰年
day++;
d1++;
if(d1>year[flag][m1-1]){
d1=1;
m1++;
}
if(m1>12){
m1=1;
y1++;
}
}
cout <<week[day%7]<<endl;;
}
}