usaco Friday the Thirteenth

来源:互联网 发布:热化学数据库建库情况 编辑:程序博客网 时间:2024/05/21 15:02
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
这个题写了好久啊啊啊!明明是一道水题,还是自己太垃圾,但是最后没问别人自己找到问题所在还是很开心的!
/*ID:PROG: fridayLANG: C++*/#include<iostream>#include<cstdio>#include<fstream>int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};int day[7]={0};int main(){    freopen("friday.in","r",stdin);    freopen("friday.out","w",stdout);    int n;    int m=6;    scanf("%d",&n);    for(int i=0;i<n;i++){        int q=1900+i;        if((q%4==0&&q%100!=0)||(q%400)==0){            month[1]=29;        }        for(int j=0;j<12;j++){            day[(m+1)%7]++;            m+=month[j];//这个月13号和下个月13号之间的天数            m%=7;//这个月13号在星期几        }        month[1]=28;    }    for(int i=0;i<6;i++){        printf("%d ",day[i]);    }    printf("%d\n",day[6]);    return 0;}
这是通过每个月13号和第一个月13号之间的天数来判断每个月的13号是星期几。注意即使是年份变了(从1900到1901)m+=month[i]依然是从前一年的12月13号到今年的1月13号,是具有连续性的!
之前的代码:
int m=0;
for(int i=0;i<n;i++){        int q=1900+i;        int m=0;        if((q%4==0&&q%100!=0)||(q%400)==0){            month[2]=29;        }        for(int j=0;j<12;j++){            m+=month[j];            int t=(m+13)%7;            day[(t+1)%7]++;        }        month[2]=28;    }
当时想的是:算出每一天到1990.1.1的天数,但是发现只有n=1时计算结果是正确的,这是因为当1900年的12个月算完后,m的值是11个月的和,到了1901年,m=11个月天数+0,显然是不对的,这时m应该为1900年的天数总和才对,因此,这种做法是错误的。
还有要注意的是,要加month[1]=28;因为当遇到闰年后month[1]改为29,之后就一直为29了!


原创粉丝点击