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號又是星期五是一個(gè)不尋常的日子嗎?13號在星期五比在其他日子少嗎?為了回答這個(gè)問題,寫一個(gè)程序,
要求計(jì)算每個(gè)月的十三號落在周一到周日的次數(shù)。給出N年的一個(gè)周期,要求計(jì)算1900年1月1日--
1900+N-1年12月31日中十三號落在周一到周日的次數(shù),N為正整數(shù)且不大于400.?
這里有一些你要知道的:?
1900年1月1日是星期一.4,6,11和9月有30天.其他月份除了2月都有31天.閏年2月有29天,平年2月有28天.
年份可以被4整除的為閏年(1992=4*498 所以 1992年是閏年,但是1990年不是閏年)以上規(guī)則不適合于世
紀(jì)年.可以被400整除的世紀(jì)年為閏年,否則為平年.所以,1700,1800,1900和2100年是平年,而2000年是閏
年.請不要預(yù)先算好數(shù)據(jù)!
格式:
PROGRAM NAME: friday?
INPUT FORMAT:?
(file friday.in)?
一個(gè)整數(shù)n.?
OUTPUT FORMAT:?
(file friday.out)?
七個(gè)在一行且相分開的整數(shù),它們代表13日是星期六,星期日,星期一...星期五的次數(shù).?
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]是周五,因?yàn)?900年的1月13日是周六,所以一開始m[0]初始化為1
11
12 const short dn[12]={3,0,3,2,3,2,3,3,2,3,2,3};
13 //這里實(shí)際上是每個(gè)月的天數(shù)(2月取28天)除以7取余之后的數(shù)為了方便運(yùn)算
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記錄上個(gè)月的13號是星期幾.. last=0代表星期六,依次類推
48
49 if ((year!=1900+n-1)||(month!=11))
50 m[last]++;
51 }//每循環(huán)一遍,是從當(dāng)年的二月到次年的一月,所以需要最后的if防止加上最后的一個(gè)一月
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 }
轉(zhuǎn)載于:https://www.cnblogs.com/isfight/archive/2011/10/26/2225469.html
總結(jié)
以上是生活随笔為你收集整理的USACO 1.1 Friday the Thirteenth的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL执行外部sql脚本文件的命令(
- 下一篇: 用gson得JSON,数值变为doubl