标签:style blog io ar color os sp for strong
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
===========================================
一看题目的时候,以为让我统计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