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

 

代码:

 1 //用一个变量last表示当前是这个月的1号是第多少天,初始为1,
 2 //一上来+12表示这个月的13号,mod 7则能计算出此时为周几,计入答案
 3 //根据是否为闰年的二月来把last加至下个月的一号
 4 #include <iostream>
 5 #include <cstdio>
 6 #include <cstring>
 7 using namespace std;
 8 int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 9 int w[8];
10 int run(int x)
11 {
12     if(x%4==0&&x%100!=0) return 1;
13     if(x%400==0) return 1;
14     return 0;
15 }
16 int main()
17 {
18     freopen("friday.in","r",stdin);
19     freopen("friday.out","w",stdout);
20     memset(w,0,sizeof(w));
21     int n;
22     cin>>n;
23     int last=1;
24     for(int i=0;i<n;i++){
25         for(int j=1;j<=12;j++){
26             last+=12;
27             w[last%7]++;
28             if(j==2&&run(1900+i)) last+=29-12;
29             else last+=m[j]-12;
30         }
31     }
32     cout<<w[6]<<" "<<w[0]<<" "<<w[1]<<" "<<w[2]<<" "<<w[3]<<" "<<w[4]<<" "<<w[5]<<endl;
33     return 0;
34 }

 

posted on 2016-01-23 15:15  Sunny糖果  阅读(187)  评论(0编辑  收藏  举报