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 此题意思是计算从1900年到某年所有每个月的13号的星期几,并统计起来。数字不大,一般不会超时。/* ID:ytjiang1 PROB:friday LANG: C++ */ /*本题计算从1900年1月1日到1900+n-1年12月31日,每个月的13号是星期几。并统计出来 因为总共不超过300年,共3600个月,遍历即可* 建立一个数组存储星期 xq[0]是星期六*/ //判断月份 判断闰年 #include <iostream> #include<fstream> #include<string> #include<string.h> using namespace std; bool runnian(int y) { if(y % 4 != 0) return false; if(y % 100 ==0) { if(y %400 ==0) return true; else return false; } return true; } int yue(int y,int m) { switch(m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;break; case 2: if(runnian(y)) return 29; return 28; default: return 30; } } int main() { ifstream fin("friday.in"); ofstream fout("friday.out"); int num; fin>>num; int xing[7]; int sss = 1; memset(xing,0,sizeof(xing)); for(int y = 1900; y<1900 +num; y++) { for(int m = 1; m<13; m++) { int day = yue(y,m); for(int d = 1; d<=day; d++) { sss = (sss +1)%7; if(d == 13) { xing[sss] += 1; } } } } for(int i = 0; i<6; i++) { fout<<xing[i]<<" "; } fout<<xing[6]<<endl; //必须要去掉最后一个空格,和加一个换行 return 0; }