尤拉計劃(Project Euler)第十九題 matlab

2020-09-28 14:00:31

尤拉計劃(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