USACO Friday the Thirteenth 解题日志

在USACO的第三题~

好久没有做题了的感觉。这几天在家里睡得好爽。。。

不得不说,这也是一道水题。


题目是这样子的:

Friday the Thirteenth

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

这不禁让我想到了SJTU的OJ上的二哥买期货的问题——很类似,问题的主要都是要计算星期几并且增加日期。幸亏当时某学长告诉我:当知道年月日时,可以用“蔡勒公式”来计算当天是星期几,否则我会陷入极大的苦恼之中。不过蔡勒公式好难记啊,每次使用我都要点开同一个百度百科来看。找个日子把它记住吧,不然要是某天考试要用到它我就gg了○| ̄|_。而使用蔡勒公式要注意的是,①当month小于等于2时,要把month变成month + 12,year变成year - 1;②为了使得经过那一串数字的计算后mod7后是非负数,要在百度百科里示例的那个公式里先 +7 再 mod 7;③当是星期天时,mod7的结果是0,不是7!(我记得我第一次和第二次使用蔡勒公式时都把这里忘了。QAQ)

这里,为了表示的方便且使表达看起来更“自然”一些,我选择把每个月的天数放在数组里表示,并且写了函数 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值