Friday解题报告

题目摘要:Is Friday the 13th really anunusual 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.


题目大意:输入一个年数N,在第1900年到第1900+N-1年之间,判断每月第十三天是周几,分别输出周日至周六有多少天


输入输出样例

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


解题思路:这题只要确定好每年每月的第一天是周几就解决了,如果是周一,那么第十三天就是周六,以此类推。

代码:

#include <iostream>

using namespace std;

int leap(int y)

{

    if((y%4==0&&y%100!=0)||y%400==0)

              return1;

    return 0;

}

int getdate(int y,int m)

{

    int w=(y+(y-1)/4-(y-1)/100+(y-1)/400)%7;

    int days=0;

    switch(m)

    {

       case12: 

days+=30;

       case11:

days+=31;

       case10: 

days+=30;

       case9:  

days+=31;

       case8:  

days+=31;

       case7:  

days+=30;

       case6:  

days+=31;

       case5:  

days+=30;

       case4:  

days+=31;

       case3:  

if(leap(y))

                              days+=29;

                      else

      days+=28;

       case2:  

days+=31;

       case1:  

days+=0;

}   

       w=(w+days)%7;

    return w;    

}

int main()

{   

       int N;   

       int year,month;

       int count;

       int Sat=0,Sun=0,Mon=0,Tues=0,Wed=0,Thurs=0,Fri=0;

       cin>>N;

       for(year=1900;year<=1900+N-1;year++)

       {

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

              {

                     count=getdate(year,month);

                     switch(count)

                     {

                     case0:

                            Fri++;

                            break;

                     case1:

                            Sat++;

                            break;

                     case2:

                            Sun++;

                            break;

                     case3:

                            Mon++;

                            break;

                     case4:

                            Tues++;

                            break;

                     case5:

                            Wed++;

                            break;

                     case6:

                            Thurs++;

                            break;

                     }

              }

       }

       cout<<Sat<<""<<Sun<<" "<<Mon<<""<<Tues<<" "<<Wed<<""<<Thurs<<" "<<Fri<<endl;

       return 0;

}


解题感想:理解了题意后我立马就想起了第一章实验题最后一题输出某年某月的日历,那题正好有一个计算某年某月第一天是周几的函数,所以直接拿来用了,只不过稍加修改,利用循环将每年每月的第一天是周几都判断一下就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值