Problem Description
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).
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.
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思路见AC代码:#include<stdio.h> int run(int m) { if((m%4==0&&m%100!=0)||(m%400==0)) return 1; else return 0; } void print(int m) { switch(m) { case 2: printf("Monday\n"); break; case 3: printf("Tuesday\n"); break; case 4: printf("Wednesday\n"); break; case 5: printf("Thursday\n"); break; case 6: printf("Friday\n"); break; case 0: printf("Saturday\n"); break; case 7: printf("Saturday\n"); break; case 1: printf("Sunday\n"); break; } } int main() { int a[]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; int dup,dn; int y,m,d; int sd,i; //天数总差 int weak,x; dn=0; for(i=1; i<11; i++) dn=dn+a[i]; dn=dn+17; dup=0; dup=dup+a[12]+13; int ffff; while(scanf("%d%d%d",&y,&m,&d)!=EOF) { ffff=0; if(0<y&&y<10000&&0<m&&m<13&&0<d&&d<32) //细节很重要,这里WA好几次.... { if(d<=a[m]||(m==2&&d<=29&&run(y))) { ffff=1; } } sd=0; if(ffff) { if(y>2007) { x=y-2007-1; sd=sd+x*365; for(i=2007; i<y; i++) { if(run(i)) sd++; } for(i=1; i<m; i++) sd=sd+a[i]; //加上月 sd=sd+d; //加上天 if(m>2&&run(y)) //如果本年是闰年且月份大于2 sd++; sd=sd+dup; weak=(sd)%7; //一周7天,因为2007,11,17星期六,所以sd+1 print(weak); } else if((y==2007&&m>11)||(y==2007&&m==11&&d>=17)) { if(m>11) sd=sd+13+d; else sd=sd+d-17; weak=(sd)%7; //一周7天,因为2007,11,17星期六,所以sd+1 print(weak); } else if(y<2007) { x=2007-(y+1); sd=sd+x*365; for(i=y+1; i<2007; i++) { if(run(i)) sd++; } for(i=m+1; i<=12; i++) sd=sd+a[i]; if(m<=2&&run(y)) sd++; sd=sd+(a[m]-d); sd=sd+dn; weak=7-(sd)%7; //一周7天,因为2007,11,17星期六,所以sd+1 print(weak); } else if((y==2007&&m<11)||(y==2007&&m==11&&d<17)) { if(m<11) { for(i=m+1; i<11; i++) sd=sd+a[i]; sd=sd+(a[m]-d)+17; } else { sd=sd+(17-d); } weak=7-(sd)%7; //一周7天,因为2007,11,17星期六,所以sd+1 print(weak); } } else { printf("illegal\n"); } } return 0; }再上一个公式:
int whatday(int y,int m,int d) //蔡勒公式.注意:只适用1582-10-5之后 { int ans ; if(m==1||m==2) m=m+12,y--; if((y<1752)||(y==1752&&m<9)||(y==1752&&m==9&&d<3)) ans=(d+2*m+3*(m+1)/5+y+y/4+5)%7; //1752-9-3之前 else ans=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; //1752-9-3之后(包括9-3) return ans; //返回值+1就是对应星期几 }