您的位置:首页 > 其它

[USACO 1.1.3] Friday the Thirteenth

2011-11-20 10:19 295 查看
[题目描述]

Friday the Thirteenth

黑色星期五

13号又是星期五是一个不寻常的日子吗?13号在星期五比在其他日少吗?为了回答这个问题,写一个程序来计算在n年里13日落在星期一,星期二......星期日的次数.这个测试从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

一个整数n.

SAMPLE INPUT (file friday.in)

20

OUTPUT FORMAT

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

SAMPLE OUTPUT (file friday.out)

36 33 34 33 35 35 34

[解题思路]

依旧是模拟,只需要注意闰年闰月的处理即可。

[Code]

{
ID: zane2951
PROG: friday
LANG: PASCAL
}

program friday;
const
days:array[1..12] of longint=(31,28,31,30,31,30,31,31,30,31,30,31);

var
ans:array[0..8] of longint;
n,goal,year,mon,now,i,ce:longint;

//----------main-----------
begin
assign(input,'friday.in'); reset(input);
assign(output,'friday.out'); rewrite(output);
readln(n); goal:=1900+n-1;
year:=1900; now:=0;
while year<=goal do
begin
mon:=1;
while mon<=12 do
begin
ce:=days[mon];
if year mod 100=0 then
begin
if (year mod 400=0) and (mon=2) then inc(ce);
end
else if year mod 4=0 then if mon=2 then inc(ce);
for i:=1 to ce do
begin
inc(now);
if now>7 then now:=1;
if i=13 then inc(ans[now]);
end;
inc(mon);
end;
inc(year);
end;
for i:=6 to 7 do write(ans[i],'':1);
for i:=1 to 4 do write(ans[i],'':1);
writeln(ans[5]);
close(input); close(output);
end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: