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号落在周一到周日的次数;
思路&易错点:判断是否为闰年,天数 mod 7作为计数数组的下标,自增1,计数数组得到的就是13号落在周日到周六的次数。有一个注意的细节,某年的一月一日是周m,即下一年的一月一日是周m + 1(闰年的话就是m + 2,开始我也没注意这个问题),继续mod下一年。
代码如下:/*
TASK:friday
LANG:C++
ID:huibaochen
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
freopen ("friday.in", "r", stdin);
freopen ("friday.out", "w", stdout);
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int w[7], day, mod, n, last, k;
while(scanf("%d", &n) != EOF) {
memset(w, 0, sizeof(w));
last = 0;
for(int j = 1900; j < 1900 + n; j++){
mod = k = 0;
day = last;
if(((j % 4 == 0) && ( j % 100 != 0)) || ( j % 400 == 0)){ //判断是否为闰年
month[1] = 29;
k = 1;
}
else
month[1] = 28;
for(int i = 0; i < 12; i++){ //计算某一年中13号落在周一到周日的次数
if(i == 0)
day = day + 13;
else
day = day + month[i - 1];
mod = day % 7;
w[mod]++;
}
if(k == 1) //某年的一月一号为周m,那么下一年的一月一号为周m +1,例如1900.1.1是周一,1901.1.1是周二,所以下一年的day重last算起,闰年要+2;
last = last + 2;
else
last++;
last = last % 7;
}
printf("%d ", w[6]);
for(int i = 0; i < 6; i++)
printf("%d%c", w[i], i == 5 ? '\n' : ' ');
}
return 0;
}
/*
TASK:friday
LANG:C++
ID:huibaochen
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
freopen ("friday.in", "r", stdin);
freopen ("friday.out", "w", stdout);
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int w[7], day, mod, n, last, k;
while(scanf("%d", &n) != EOF) {
memset(w, 0, sizeof(w));
last = 0;
for(int j = 1900; j < 1900 + n; j++){
mod = k = 0;
day = last;
if(((j % 4 == 0) && ( j % 100 != 0)) || ( j % 400 == 0)){ //判断是否为闰年
month[1] = 29;
k = 1;
}
else
month[1] = 28;
for(int i = 0; i < 12; i++){ //计算某一年中13号落在周一到周日的次数
if(i == 0)
day = day + 13;
else
day = day + month[i - 1];
mod = day % 7;
w[mod]++;
}
if(k == 1) //某年的一月一号为周m,那么下一年的一月一号为周m +1,例如1900.1.1是周一,1901.1.1是周二,所以下一年的day重last算起,闰年要+2;
last = last + 2;
else
last++;
last = last % 7;
}
printf("%d ", w[6]);
for(int i = 0; i < 6; i++)
printf("%d%c", w[i], i == 5 ? '\n' : ' ');
}
return 0;
}

本文提供了一个程序,用于计算从1900年到N年后每一年每个月的第13天是星期几的频率,考虑了闰年的规则。
430

被折叠的 条评论
为什么被折叠?



