Problem 19
- 投稿者 : rei
Original
You are given the following information, but you may prefer to do some research for yourself.
- 1 Jan 1900 was a Monday.
- Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.- A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
和訳
次の情報が与えられている。
- 1900年1月1日は月曜日である。
- 9月、4月、6月、11月は30日まであり、2月を除く他の月は31日まである。
- 2月は28日まであるが、うるう年のときは29日である。
- うるう年は西暦が4で割り切れる年に起こる。しかし、西暦が400で割り切れず100で割り切れる年はうるう年でない。
20世紀(1901年1月1日から2000年12月31日)で月の初めが日曜日になるのは何回あるか。
当てにならないソースコード(C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; namespace ProjectEuler { class Problem19 : Problem{ public Problem19() { DateTime date = new DateTime(1901, 1, 1); int answer = 0; for (int i = 0; i < 1200; i++) { if (date.DayOfWeek == DayOfWeek.Sunday) ++answer; date = date.AddMonths(1); } Console.WriteLine("> " + answer); } } } |
大学の課題でCでカレンダーを表示させたりしてたのを思い出しました。
そのときはちゃんと自分で計算してましたよ。

rei@sikios.com
コメントはまだありません。