这道题挺不好写的,题目是说给出一个年份范围,统计这个范围内星期一到星期日为 13 号的个数。
/* PROG: friday LANG: C++ */ # include <cstdio> const char daytab[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 isLeap(int year) { return year%4 == 0 && year%100 != 0 || year%400 == 0 ? 1 : 0; } int dayOfMonth(int year, int day) { int leap, i; leap = isLeap(year); for (i = 1; day > daytab[leap][i]; ++i) day -= daytab[leap][i]; return day; } int main() { int n; freopen("friday.in", "r", stdin); freopen("friday.out", "w", stdout); scanf("%d", &n); int f[7]; for (int i = 0; i < 7; ++i) f[i] = 0; int day = 0, wk = 0; for (int year = 1900; year < 1900+n; ) { wk = (wk+1) % 7; ++day; if (dayOfMonth(year, day) == 13) ++f[wk]; if ((day == 365 && isLeap(year) == 0) || (day == 366 && isLeap(year) == 1)) { ++year; day = 0; } } printf("%d", f[6]); for (int i = 0; i < 6; ++i) printf(" %d", f[i]); printf("\n"); fclose(stdin); fclose(stdout); return 0; }
/**/