原题:
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.
题意:
1900年一月一日是星期一,计算从1900到1900+n-1年的12月31日每个月的十三号是星期几,分别统计并输出(1<=n<=400)
输入要求:
n
输出要求:
注意:是周六 日 一 二 三 四 五的格式
题解:遍历统计就行
代码:
/* ID:newyear111 PROG: friday LANG: C++ */#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=13;
//记录闰年和正常年份的月天数
int days[2][N]={
0,31,28,31, 30,31,30, 31,31,30, 31,30,31, 0,31,29,31, 30,31,30, 31,31,30, 31,30,31};
//记录答案
int ans[N];
//判断是否为闰年
bool isR(int year)
{ return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}int main()
{ifstream fin("friday.in");ofstream fout("friday.out");int n;fin>>n;//记录从1900年到现在的的天数 int numOfDay=0;for(int i=0;i<n;i++){int flag=isR(1900+i);for(int j=1;j<=12;j++){ans[(numOfDay+12)%7+1]++; numOfDay+=days[flag][j]; } }//注意输出的顺序 fout<<ans[6]<<" "<<ans[7]<<" "<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "<<ans[4]<<" "<<ans[5]<<endl;fin.close();fout.close();return 0;
}