欧拉计划(Project Euler)第十九题 matlab
原题目:
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日为星期一;
- 一三五七八十腊是31天,其余除了二月,都是30天;
- 二月除了闰年都是28天,闰年29天。2000年虽然是世纪年,但是能被400整除,故也是闰年。
计算范围是1901年一月1日到2000年十二月31日
sum=1;
a=[31 28 31 30 31 30 31 31 30 31 30 31];
b=0;
for year=1901:2000
for i=1:12
if mod(year,4)==0
a(2)=29;
end
sum=sum+a(i);
if mod(sum,7)==5
b=b+1;
end
end
end
b
最后输出结果是171