usaco 1.1 Friday the thirteenth

http://hi.baidu.com/raulliubo/blog/item/356d8aef929862efce1b3eb8.html

 

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
这又是一道简单题。。。会算哪天是星期几就行了。。。。复制粘贴就是好使。。。

this is my program

----------------------------------------------------我叫分界线------------------------------------------------------

{
ID:Parachutes
PROG:friday
LANG:PASCAL
}
var i,day,n:integer;
    s:array[0..6] of integer;
begin
assign(input,'friday.in'); reset(input);
assign(output,'friday.out'); rewrite(output);
readln(n);
day:=13;
for i:=1900 to 1899+n do
 begin
  day:=day mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day + 28 + ord((i mod 4 = 0) and ((i mod 100 <> 0) or (i mod 400 = 0)))) mod 7;//classic....by sqybi...thx..
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day+30) mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day+30) mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day+30) mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;
  inc(s[day]);
  day:=(day+30) mod 7;
  inc(s[day]);
  day:=(day+31) mod 7;//这个算法太经典了。。。。复制粘贴比想简单多了。。。哈哈。
 end;
write(s[6],' ');
for i:=0 to 4 do write(s[i],' ');
writeln(s[5]);
close(input);
close(output);
end.
呵呵。。。感谢sqybi的经典算法。。

posted @ 2009-01-04 13:02  jesonpeng  阅读(193)  评论(0编辑  收藏  举报