判断闰年:(y%4==0 && y%100!=0) || (y%400==0) 没用算法,一个个累计,还可改进。 /* ID: gengjia1 LANG: C TASK: friday */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> enum week{Sat, Sun, Mon, Tue, Wed, Thu, Fri}; typedef struct { int year; int month; int day; enum week weekday; }time; int main(void) { FILE *fin = fopen ("friday.in", "r"); FILE *fout = fopen ("friday.out", "w"); int total_year, total_month = 12, total_day; int i; int counts[7] = {0}; /*第一天是Saturday*/ fscanf (fin, "%d", &total_year); assert(total_year <= 400); /*时间初始化*/ total_year += (1900 - 1); time t = {1900, 1, 1, Mon}; for(t.year = 1900; t.year <= total_year; t.year++) { for(t.month = 1; t.month <= total_month; t.month++) { switch(t.month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: total_day = 31; break; case 4: case 6: case 9: case 11: total_day = 30; break; default: { if((t.year%4==0 && t.year%100!=0) || (t.year%400==0)) total_day = 29; else total_day = 28; } break; } for(t.day = 1; t.day <= total_day; t.day++) { if (t.day == 13){ counts[t.weekday] +=1; } if((int)t.weekday >= 6) t.weekday = Sat; else t.weekday = (enum week)((int)t.weekday + 1); } } } for(i = 0; i < 6; i++) { fprintf (fout, "%d ", counts[i]); } fprintf (fout, "%d/n", counts[i]); fclose(fin); fclose(fout); exit(0); }