http://acm.hdu.edu.cn/showproblem.php?pid=2133
蔡勒公式求某一天是周几。
View Code
1 /*** 蔡勒公式,求某一天是周几(hdu 2133)***/ 2 #include <cstdio> 3 4 const char *ans[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 5 6 int isLeap(int y) { // 判断是否闰年 7 return !(y % 4) && (y % 100) || !(y % 400); 8 } 9 10 int mod(int a, int b) { // 保证取模后是正数 11 return (a % b + b) % b; 12 } 13 int main() { 14 int y, m, d, c, w; 15 16 while (~scanf("%d%d%d", &y, &m, &d)) { 17 if (d <= 0 || m <= 0 || d >= 10000 || m > 12 || d > 31) { 18 printf("illegal\n"); 19 continue; 20 } 21 if (!isLeap(y) && m == 2 && d > 28) { 22 printf("illegal\n"); 23 continue; 24 } 25 if (isLeap(y) && m == 2 && d > 29) { 26 printf("illegal\n"); 27 continue; 28 } 29 if ((m == 4 || m == 6 || m == 9 || m == 11) && d > 30) { 30 printf("illegal\n"); 31 continue; 32 } 33 if (m == 1 || m == 2) m += 12, y--; 34 /*** 蔡勒公式 ***/ 35 c = y / 100; 36 y %= 100; 37 w = (c >> 2) - (c << 1) + y + (y >> 2) + 13 * (m + 1) / 5 + d - 1; 38 printf("%s\n", ans[mod(w, 7)]); 39 /****************/ 40 } 41 return 0; 42 }
——written by Lyon