USACO1.1.3--Friday the Thirteenth

Friday the Thirteenth

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:

  • January 1, 1900 was on a Monday.
  • Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
  • Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
  • The rule above does not hold for century years. Century years divisible by 400 are leap years, all other are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.

Do not use any built-in date functions in your computer language.

Don't just precompute the answers, either, please.

PROGRAM NAME: friday

INPUT FORMAT

One line with the integer N.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34

题解:题目要求就是要确定每个月的13号是星期几。用m累加经过月份的天数,那么就可以用公式表示出每月13号具体是星期几了,公式就是这样的:(m+13)%7。哈哈,非常简单的说。。。
View Code
 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 }
也可以用泰勒公式计算出具体是星期几。
泰勒公式
公式都是基于公历的置闰规则来考虑。
公式中的符号含义如下:
  • w:星期
  • c:世纪(前两位数)
  • y:年(后两位数)
  • m:月(m 的取值范围为 3 至 14,即在蔡勒公式中,某年的 1、2月要看作上一年的 13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
  • d:日
  • [ ]:称作高斯符号,代表取整,即只要整数部份。
  • mod:‎‎同余‎(这里代表括号里的答案除以 7 后的余数)(请注意前面是负数取模的情况,取模只可以是正数)

若要计算的日期是在1582年10月4日或之前,公式则为

(因罗马教皇修改历法,把1582年10月4日的下一天改为1582年10月15日)

 

你可能感兴趣的:(USACO)