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)?
first create a function for get the how many day in that month.
then just create 3 for loops eh? lol
_________________________here's the code__________
function getMonthDate(month,year)
if (month == 2) then
if (year % 4) == 0 and year ~= 1900 then
return 29
else
return 28
end
elseif (month == 1) or
(month == 3) or
(month == 5) or
(month == 7) or
(month == 8) or
(month == 10) or
(month == 12) then
return 31
else
return 30
end
end
function getDay(day)
if day == 1 then
return "sunday"
elseif day == 2 then
return "monday"
elseif day == 3 then
return "tuesday"
elseif day == 4 then
return "wednesday"
elseif day == 5 then
return "thursday"
elseif day == 6 then
return "friday"
elseif day == 7 then
return "saturday"
else
return "error"
end
end
d = { num = 1, day = 2, month = 1, year = 1900}
match = 0
for i = d.year, 2000 do
for j = d.month, 12 do
--print("_____________________________")
--print("the month is "..d.month)
--print ("this month has "..getMonthDate(d.month, d.year).." days")
for k = d.num, getMonthDate(d.month, d.year) do
--print("the day is "..d.num.." on "..getDay(d.day))
if (d.num == 1) and (d.day == 1) and (d.year > 1900) then
match = match + 1
end
if d.day == 7 then d.day = 0 end
if d.num == getMonthDate(d.month, d.year) then d.num = 0 end
d.day = d.day + 1
d.num = d.num + 1
end
if d.month == 12 then d.month = 0 end
d.month = d.month + 1
end
d.year = d.year + 1
end
print (match)

ไม่มีความคิดเห็น:
แสดงความคิดเห็น