在USACO的第三题~
好久没有做题了的感觉。这几天在家里睡得好爽。。。
不得不说,这也是一道水题。
题目是这样子的:
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
这里,为了表示的方便且使表达看起来更“自然”一些,我选择把每个月的天数放在数组里表示,并且写了函数 LeapMonth 来使得判断闰月的表达更简明。当然,这种表达不是我想的。。。还是要归功于那位学长○| ̄|_。
嘛,虽然是水题,但是粗心加经验不足的我仍然是在第二次提交是才通过。原因有两个:
①做题的时候就忘了只需要计算每个月的第13天是星期几,我把每天都计算了并且加到最终结果去了。(估计要是没有标准答案和我的答案的对比,我会看上“很久”才发现这个巨大的错误);
②在输出最终结果时,使用了如下for循环:
for (int i = 1; i < 8; ++i)
fout << days[i] << " ";
fout << endl;
然而,题目的要求,在输出星期五的天数后面,是没有空格的!OMG!平时做过的题都直接这样写就没事,还是我做的题太少了啊(苦笑)~
最后贴上我这道题的代码。应该可以跳天数来计算吧?但是似乎这样写起来对于我来说有些麻烦,就作罢了。
#include<iostream>
#include<fstream>
using namespace std;
const int DaysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days[8] = {0};
int LeapMonth(int y, int m)
{
if (m != 2) return 0;
if (y % 400 == 0) return 1;
if (y % 4 == 0) return 1;
if (y % 100 == 0) return 0;
return 0;
}
void WeekDay(int y, int m, int d)
{
if (m <= 2){
m += 12;
y -= 1;
}
if (d != 13) return;
int a = y % 100; int c = y / 100;
int w = ((a + a / 4 + c / 4 - 2 * c + (26 * (m + 1) / 10) + d - 1) % 7 + 7) % 7;
switch (w){
case 6: ++days[1]; break;
case 0: ++days[2]; break;
case 1: ++days[3]; break;
case 2: ++days[4]; break;
case 3: ++days[5]; break;
case 4: ++days[6]; break;
case 5: ++days[7]; break;
}
}
int main()
{
ifstream fin("friday.in");
ofstream fout("friday.out");
int N;
int y1 = 1900, m1 = 1, d1 = 1;
fin >> N;
int y2 = 1900 + N - 1, m2 = 12, d2 = 31;
while (N != 0){
WeekDay(y1, m1, d1);
++d1;
if (d1 > DaysInMonth[m1] + LeapMonth(y1, m1)){
d1 = 1;
++m1;
}
if (m1 > 12){
++y1;
--N;
m1 = 1;
}
}
for (int i = 1; i < 7; ++i)
fout << days[i] << " ";
fout << days[7];
fout << endl;
return 0;
}