关闭

USACO firday 模拟

标签: outputintegerfilerestlessbing
871人阅读 评论(0) 收藏 举报
分类:
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 non-negative and will not exceed 400.

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: topcoder3

PROG: friday

LANG: C++

*/

 

#include<fstream>

 

using namespace std;

 

int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int ans[7];

 

ifstream fin("friday.in");

ofstream fout("friday.out");

 

int main()

{

         int n,i,d=0,y,c=0,t,j;

         fin>>n;

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

         {

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

                   {

                            m[1]=29;

                            y=366;

                   }

                   else

                   {

                            m[1]=28;

                            y=365;

                   }

                   ans[(13+c)%7]++;

                   t=13+c;

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

                   {

                            t=t+m[j-1];

                            ans[t%7]++;

                   }

                   d=y+c;

                   c=d%7;

         }

         fout<<ans[6]<<" "<<ans[0]<<" "<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "<<ans[4]<<" "<<ans[5]<<endl;

         return 0;

}

数据:

USER: bing shen [topcode3]
TASK: friday
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3012 KB]
   Test 2: TEST OK [0.000 secs, 3012 KB]
   Test 3: TEST OK [0.000 secs, 3012 KB]
   Test 4: TEST OK [0.022 secs, 3012 KB]
   Test 5: TEST OK [0.011 secs, 3012 KB]
   Test 6: TEST OK [0.000 secs, 3012 KB]
   Test 7: TEST OK [0.000 secs, 3012 KB]
   Test 8: TEST OK [0.000 secs, 3012 KB]

All tests OK.

Your program ('friday') produced all correct answers! This is your submission #2 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 -------
1
------- test 2 -------
2
------- test 3 -------
5
------- test 4 -------
13
------- test 5 -------
45
------- test 6 -------
100
------- test 7 -------
256
------- test 8 -------
400
Keep up the good work!

Thanks for your submission!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:385138次
    • 积分:6542
    • 等级:
    • 排名:第3629名
    • 原创:239篇
    • 转载:0篇
    • 译文:0篇
    • 评论:90条
    最新评论