码迷,mamicode.com
首页 > 其他好文 > 详细

1.1.5 PROB Friday the Thirteenth

时间:2014-11-30 13:49:01      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   strong   

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

===========================================

一看题目的时候,以为让我统计1900~1900+n-1中,星期一~星期天有多少天。

然后码完了才发现。。。是统计13号这一天,我也是醉了

处理策略相当简单,下面是AC代码

 1 /*
 2 ID: luopengting
 3 PROG: friday
 4 LANG: C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 using namespace std;
10 const int maxn = 405;
11 int year[maxn];
12 int days[8];
13 int m[] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};  //记得12月份摆在第一
14         // 12, 1 , 2,  3,  4,  5,  6,  7,  8,  9,  10, 11
15 int tt;
16 void init()
17 {
18     for(int i = 1900; i < 1900 + maxn; i++)
19     {
20         if((i % 400 == 0) || ((i % 100) && (i % 4 == 0)))
21         {
22             year[i - 1900] = 1;
23         }
24         else
25         {
26             year[i - 1900] = 0;
27         }
28     }
29 }
30 void deal(int n)
31 {
32     tt = - 31;
33     for(int i = 0; i < n; i++)
34     {
35         for(int j = 0; j < 12; j++)
36         {
37             tt += m[j];
38             if(year[i] && j == 2)//闰年
39             {
40                 tt++;
41             }
42             tt %= 7;
43             days[tt]++;
44         }
45     }
46 }
47 int main()
48 {
49     freopen("friday.in","r",stdin);
50     freopen("friday.out","w",stdout);
51     init();
52     int n;
53     while(cin >> n)
54     {
55         memset(days, 0, sizeof(days));
56         deal(n);
57         int i;
58         for(i = 0; i < 6; i++)
59         {
60             cout << days[i] << " ";
61         }
62         cout << days[i] << endl;
63     }
64     return 0;
65 }

 

1.1.5 PROB Friday the Thirteenth

标签:style   blog   io   ar   color   os   sp   for   strong   

原文地址:http://www.cnblogs.com/imLPT/p/4132810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!