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
题解
- 输入的月份是字符串,所以要将字符串月份处理成int
- 用蔡勒公式计算星期几:
W=[C/4]-2C+y+[y/4]+[26(m+1)/10]+d-1 (其中[ ]为取整符号)
注意:
1、 C是年份的前两位数
y是年份的后两位数
m是月份
d是日
2、需要注意的是如果月份是一月二月,则需看成是上一年的13月,14月,所以月份不是1-12,而是3-14。
3、W算出来的数有可能是负数,这是需要将他加上一个较大的7的倍数,然后进行取余。
4、也就是说W:0-6,然后根据取值选择星期。
c++11
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
char mon[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char week[7][10]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
//在这个模板的同一级目录下建一个'1.txt'的文件
//然后把输入全部放进去保存,之后运行代码完全不用输入任何数据
#endif
int d,y,w;
char sm[10];
while (cin>>d>>sm>>y){
int i; //i在for外int,因为下面的int m=i+1;也需要
for (i=0;i<12;i++){
if(!strcmp(sm,mon[i]))
break;
}
//下面是蔡勒公式
int m=i+1;
if(m==1||m==2){
m+=12;
y--;
}
w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
cout<<week[w]<<endl;
}
return 0;
}
感想
为啥机试也要去背这种公式,我哭辽,如果能让用python机试多好