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 non-negative and will not exceed 400.

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


由于看错题,可花了我好长一段时间。
View Code
 1 /*
2 ID:10239512
3 PROG:friday
4 LANG:C++
5 */
6
7 #include<iostream>
8 #include<fstream>
9 using namespace std;
10 ifstream fin("friday.in");
11 ofstream fout("friday.out");
12
13 int n,day,days,year=1900,month,num[8]={0};int x[8]={0,1,3,5,7,8,10,12};
14
15 bool leap(int y){
16 if((y%4==0&&y%100!=0)||y%400==0)
17 return 1;
18 return 0;
19 }
20 bool check(int y){
21 int i;
22 for(i=1;i<=7;i++)
23 if(x[i]==y) return 0;
24 return 1;
25 }
26
27 int main()
28 {
29 int i,j,k,l;
30 fin>>n;
31 day=1;
32 while(year<1900+n)
33 {
34 month=0;
35 while(month<=11)
36 {
37 month++;days=1;
38 if(month==2&&leap(year)!=1)
39 {
40 while(days<=28)
41 {
42 days++;
43 day++;
44 if(day>7) day=1;
45 if(days==13) num[day]++;
46 }
47 continue;
48 }
49
50 if(month==2&&leap(year)==1)
51 {
52 while(days<=29)
53 {
54 days++;
55 day++;
56 if(day>7) day=1;
57 if(days==13) num[day]++;
58 }
59 continue;
60 }
61
62 if(check(month))
63 {
64 while(days<=30)
65 {
66 days++;
67 day++;
68 if(day>7) day=1;
69 if(days==13) num[day]++;
70 }
71 continue;
72 }
73
74 if(!check(month))
75 {
76 while(days<=31)
77 {
78 days++;
79 day++;
80 if(day>7) day=1;
81 if(days==13) num[day]++;
82 }
83 continue;
84 }
85
86 }
87 year++;
88 }
89
90 fout<<num[6]<<" "<<num[7]<<" ";
91 for(i=1;i<=4;i++)
92 fout<<num[i]<<" ";
93 fout<<num[5];
94 fout<<endl;
95 return 0;
96
97 }
posted on 2012-02-26 15:05  怡红公子  阅读(220)  评论(0编辑  收藏  举报