题目描述
Today is Saturday, 17th Nov,2007. Now, if i tell you a date, can you tell me what day it is ?
Input
There are multiply cases.
One line is one case.
There are three integers, year(0<year<10000), month(0<=month<13), day(0<=day<32).
Output
Output one line.
if the date is illegal, you should output "illegal". Or, you should output what day it is.
Sample Input
2007 11 17
Sample Output
Saturday
解题思路
嗯...就是用一个公式 ,基姆拉尔森计算公式.
m 代表月份 d 代表日期 y代表年份
把当年的1月和2月当做上一年的13月和14月计算
公式 w= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7
所得的w: 0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
另外,遇到年份月份这种问题,用数组写比较方便一点,用数组存放每月的天数.
代码
#include<stdio.h>
#include<string.h>
int leep[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int chick(int year){
if( ((year%4==0&&year%100!=0)||year%400==0) ) return 1;
else return 0;
}
char s[10][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int main(){
int y,m,d,w;
while(~scanf("%d%d%d",&y,&m,&d)){
if(m==0){
printf("illegal\n");continue;
}
if(d<=0||d>leep[chick(y)][m]){
printf("illegal\n");continue;
}
if(m==1||m==2){
m+=12;y--;
}
w= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7;
puts(s[w]);
}
return 0;
}