Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
Note that the start year is NINETEEN HUNDRED, not 1990.
There are few facts you need to know before you can solve this problem:
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
One line with the integer N.
20
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
36 33 34 33 35 35 34
题解:题目要求就是要确定每个月的13号是星期几。用m累加经过月份的天数,那么就可以用公式表示出每月13号具体是星期几了,公式就是这样的:(m+13)%7。哈哈,非常简单的说。。。
1 /* 2 ID:spcjv51 3 PROG:friday 4 LANG:C 5 */ 6 #include<stdio.h> 7 const int month[2][13]= {{0,31,28,31,30,31,30,31,31,30,31,30,31}, 8 {0,31,29,31,30,31,30,31,31,30,31,30,31} 9 }; 10 int days[7]= {0,0,0,0,0,0,0}; 11 int main(void) 12 { 13 FILE *fin=fopen("friday.in","r"); 14 FILE *fout=fopen("friday.out","w"); 15 long int i,j,k,m,n; 16 fscanf(fin,"%d",&n); 17 m=0; 18 for(i=1900; i<1900+n; i++) 19 { 20 k=(i%400==0||(i%4==0&&i%100!=0))?1:0; 21 for(j=1; j<13; j++) 22 { 23 days[(m+13)%7]++; 24 m+=month[k][j]; 25 } 26 } 27 fprintf(fout,"%d",days[6]); 28 for(i=0; i<6; i++) 29 fprintf(fout," %d",days[i]); 30 fprintf(fout,"\n"); 31 fclose(fin); 32 fclose(fout); 33 return 0; 34 }
也可以用泰勒公式计算出具体是星期几。
公式都是基于公历的置闰规则来考虑。
公式中的符号含义如下:
若要计算的日期是在1582年10月4日或之前,公式则为
(因罗马教皇修改历法,把1582年10月4日的下一天改为1582年10月15日)