第一层第三题:黑色星期五

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 onany other day of the week? To answer this question, write a program that willcompute the frequency that the 13th of each month lands on Sunday, Monday,Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of Nyears. 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. Theseintegers 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

 

 

       题目大意是讲不少西方人认为每月的13号不吉利,星期五也不吉利,所以如果某个月的13号是星期五的话那么应该是一件灰常罕见的事情。So,任务来了,写一个程序,输入一个数字N,输出从1900年1月1号到(1900+N-1)年12月31号中,星期一恰逢13号的有几天,星期二恰逢13号的有几天。。。以此类推。

注意:

       1、坑爹的题目要求先输出星期六恰逢13号的天数,星期日恰逢13号的天数,星期一。。。星期五恰逢13号的天数,以此类推,而不是我们熟悉的从星期一到星期日,或者从星期日到星期六。

       2、注意平年和闰年。

}

       继续模拟,一个变量记录星期几,一个变量记录几号,一个变量记录几年,然后就是各种判断越界。。。

 

/*
ID:su1q2d21
LANG:C++
TASK:friday
*/
#include<iostream>
#include<cstdio>
#define maxmonth 13
#define maxweekdays 8
using namespace std;
int ans[maxweekdays];
int days[maxmonth][2];
void get_day()
{
	int i;
	days[1][0]=31;
	days[2][0]=28;
	days[3][0]=31;
	days[4][0]=30;
	days[5][0]=31;
	days[6][0]=30;
	days[7][0]=31;
	days[8][0]=31;
	days[9][0]=30;
	days[10][0]=31;
	days[11][0]=30;
	days[12][0]=31;
	for(i=1;i<=12;i++) days[i][1]=days[i][0];
	days[2][1]++;
	for(i=1;i<=7;i++) ans[i]=0;
	return ;
}
int main()
{
	freopen("friday.in","r",stdin);
	freopen("friday.out","w",stdout);
	int n;
	int nw;
	int ty;
	int nd;
	int nm;
	int ny;
	int lp;
	scanf("%d",&n);
	nw=1;
	ty=1900+n-1;
	nd=1;
	nm=1;
	lp=0;
	ny=1900;
	get_day();
	while(1){
		if(nd==13) ans[nw]++;
		nd++;
		nw++;
		if(nw>7) nw=1;
		if(nd>days[nm][lp]){
			nm++;
			nd=1;
		}
		if(nm>12){
			ny++;
			nm=1;
		}
		if(ny%4==0 && (ny%100!=0 || ny%400==0)) lp=1;
		else lp=0;
		if(ny>ty) break;
	}
	printf("%d %d %d %d %d %d %d\n",ans[6],ans[7],ans[1],ans[2],ans[3],ans[4],ans[5]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值