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模拟代码:#include<iostream> using namespace std; char day[7][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; bool isl(int y) { if(y%4==0&&y%100!=0||y%400==0) return 1; return 0; } int main() { int y,m,d,i; while(cin>>y>>m>>d) { if(m==0||d==0||(m==2&&d>29)||!isl(y)&&m==2&&d>28) { cout<<"illegal"<<endl; continue; } if(m==4||m==6||m==9||m==11) { if(d>30) { cout<<"illegal"<<endl; continue; } } if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) { if(d>31) { cout<<"illegal"<<endl; continue; } } int sum,co=0,k; for(i=1;i<y;i++) { if(isl(i)) co++; } if(isl(y)) k=1; else k=2; for(i=1;i<m;i++) { if(i==1||i==3||i==5||i==7||i==8||i==10||i==12) co++; else if(i==2) co-=k; } sum=365*(y-1)+30*(m-1)+co+d; cout<<day[sum%7]<<endl; } return 0; }
发现了一个叫蔡勒公式的东西:w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
c:世纪减1(年份前两位数)
y:年(后两位数)
m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d:日
W = [C/4] - 2C + y + [y/4] + [13 * (M+1) / 5] + d - 1
/*** 蔡勒公式,求某一天是周几(hdu 2133)***/ #include <cstdio> const char *ans[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int isLeap(int y) { // 判断是否闰年 return !(y % 4) && (y % 100) || !(y % 400); } int mod(int a, int b) { // 保证取模后是正数 return (a % b + b) % b; } int main() { int y, m, d, c, w; while (~scanf("%d%d%d", &y, &m, &d)) { if (d <= 0 || m <= 0 || d >= 10000 || m > 12 || d > 31) { printf("illegal\n"); continue; } if (!isLeap(y) && m == 2 && d > 28) { printf("illegal\n"); continue; } if (isLeap(y) && m == 2 && d > 29) { printf("illegal\n"); continue; } if ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) { printf("illegal\n"); continue; } if (m == 1 || m == 2) m += 12, y--; /*** 蔡勒公式 ***/ c = y / 100; y %= 100; w = (c >> 2) - (c << 1) + y + (y >> 2) + 13 * (m + 1) / 5 + d - 1; printf("%s\n", ans[mod(w, 7)]); /****************/ } return 0; }