USACO 1.1 Friday the Thirteenth

题目来源:USACO 1.1
原题目:
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

译题:
13号又是星期五是一个不寻常的日子吗?13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,
要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算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: 

(file friday.in) 

一个整数n. 

OUTPUT FORMAT: 

(file friday.out) 

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

View Code
 1 /*
2 ID:flyings3
3 PROB:friday
4 LANG:C++
5 */
6 #include <fstream>
7 using namespace std;
8 short n;
9 int m[7]={1,0,0,0,0,0,0,};
10 //m[0]是周六,m[6]是周五,因为1900年的1月13日是周六,所以一开始m[0]初始化为1
11
12 const short dn[12]={3,0,3,2,3,2,3,3,2,3,2,3};
13 //这里实际上是每个月的天数(2月取28天)除以7取余之后的数为了方便运算
14
15 int main()
16 {
17 ifstream fin("friday.in");
18 fin>>n;
19 fin.close();
20
21 short year,month,last=0;
22 bool rn;
23
24 for (year=1900;year<1900+n;year++)
25 {
26 if (year%100==0)
27 if (year%400==0)
28 rn=true;
29 else
30 rn=false;
31 else
32 if (year%4==0)
33 rn=true;
34 else
35 rn=false;
36 //判断是否为闰年
37
38 for (month=0;month<12;month++)
39 {
40 if (month==1) //month==1代表2月
41 if (rn)
42 last=(1+last)%7;
43 else
44 last=(dn[1]+last)%7;
45 else
46 last=(dn[month]+last)%7;
47 //last记录上个月的13号是星期几.. last=0代表星期六,依次类推
48
49 if ((year!=1900+n-1)||(month!=11))
50 m[last]++;
51 }//每循环一遍,是从当年的二月到次年的一月,所以需要最后的if防止加上最后的一个一月
52
53 }
54 ofstream fout("friday.out");
55 for (short i=0;i<6;fout<<m[i++]<<" ");
56 fout<<m[6]<<endl;
57 fout.close();
58 return 0;
59
60 }




posted @ 2011-10-26 17:21  迷茫者的旅途  阅读(271)  评论(0编辑  收藏  举报