Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often thanon any other day of the week? To answer this question, write a programthat will compute the frequency that the 13th of each month lands onSunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday overa given period of N years. The time period to test will be from January1, 1900 to December 31, 1900+N-1 for a given number of years, N. N ispositive 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 resthave 31 except for February which has 28 except in leap years when ithas 29.
- Every year evenly divisible by 4 is a leap year (1992 = 4*498 so1992 will be a leap year, but the year 1990 is not a leap year)
- The rule above does not hold for century years. Century yearsdivisible by 400 are leap years, all other are not. Thus, the centuryyears 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leapyear.
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 representthe 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号落在周几的数目,仔细观察发现, 每个月的天数有用的只是该月天数Mod7的余数,而除了2月外,每个月天数都是固定的,那么,除单独判断2月天数外,用数组存储这些数目就变的自然而然了。Of course, 每下个月的13号在那一天又依赖于上个月13号在哪一天,所以,又必须单独记录上个月13号在哪一天。 代码如下:/* ID:ayludon3 LANG: C++ TASK: friday */ #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin ("friday.in"); ofstream fout ("friday.out"); int n; int week[7]={1,0,0,0,0,0,0}; //week[0]是周六 int days[12]={3,0,3,2,3,2,3,3,2,3,2,3}; int i,year,month,last=0; bool leap; fin>>n; for (year=1900; year<1900+n; year++) { if (year%400==0||(year%100!=0&&year%4==0)) leap=1; else leap=0; for (month=0;month<12;month++) { if(month==1) if(leap) //2月是闰年 last=(1+last)%7; else last=(days[1]+last)%7; else last=(days[month]+last)%7; if ((year!=1900+n-1)||(month!=11)) //不是最后一年或者不是12月 week[last]++; } } for(i=0;i<6;i++) fout<<week[i]<<' '; fout<<week[6]<<endl; return 0; }