1.1.2---Friday the Thirteenth

探讨13号星期五是否真的较为罕见。通过编程计算指定年份范围内每个月13号落在一周各天的频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Friday the Thirteenth

Is Friday the 13th really an unusual event?

That is, does the 13th of the month land ona 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 monthlands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturdayover 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 andwill not exceed 400.

Note that the start year is NINETEENHUNDRED, not 1990.

There are few facts you need to know beforeyou can solve this problem:

January 1, 1900 was on a Monday.

Thirty days has September, April, June, andNovember, all the rest have 31 except for February which has 28 except in leapyears when it has 29.

Every year evenly divisible by 4 is a leapyear (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leapyear)

The rule above does not hold for centuryyears. 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 aleap year.

Do not use any built-in date functions inyour 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


13号又是星期五是一个不寻常的日子吗?

13号在星期五比在其他日少吗?为了回答这个问题,写一个程序来计算在n年里13

日落在星期一,星期二......星期日的次数.这个测试从1900年1月1日到

1900+n-1年12月31日.n是一个非负数且不大于400.

这里有一些你要知道的:

1900年1月1日是星期一.

4,6,11和9月有30天.其他月份除了2月有31天.闰年2月有29天,平年2月有28天.

年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年)

以上规则不适合于世纪年.可以被400整除的世纪年为闰年,否则为平年.所以,1700,1800,1900和2100年是平年,而2000年是闰年.

请不要预先算好数据!

PROGRAM NAME: friday

INPUT FORMAT

一个整数n.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一.....星期五的次数.

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34

/*

ID: ******

PROG: friday

LANG: C++

*/

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

int main()

{

   ofstream fout ("friday.out");

   ifstream fin ("friday.in");

         intn,i,j,k,day;

         intst[8];

         memset(st,0,sizeof(st));//数组置0

         fin>>n;

         day=0;

         for(i=1900;i<=1900+n-1;i++)

                   for(j=1;j<=12;j++)

                            for(k=1;k<=31;k++)

                            {

                                     day++;

                                     if(k==13)//每月13号

                                               st[day%7]++;

                                     if((j==4||j==6||j==9||j==11)&&k==30)//每月仅30天的月份

                                               break;

                                     if(!((i%4==0&&i%100!=0)||i%400==0)&&j==2&&k==28)//非闰年2月有28天

                                               break;

                                     if(j==2&&k==29)//闰年2月29天

                                               break;

                                    

                            }

        

         fout<<st[6]<<"";

    for(i=0;i<=4;i++)

         fout<<st[i]<<"";

          fout<<st[5]<<endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值