尝试编写经典的脑筋急转弯储物柜问题(打开/关闭 100 个储物柜的问题)。当我运行我编写的代码时,它并没有给出 10 个打开的储物柜的正确答案,而是说所有储物柜都已关闭。我想我在循环中遗漏了一些东西……有什么建议吗?谢谢。
def lockerproblem
j = 0
lockers = []
while j < 100
lockers << "open"
j += 1
end
a = 1
i = 0
while a <= 100
while i < 100
if ( i + 1 ) % a == 0
if lockers[i] == "open"
lockers[i] = "closed"
else
lockers[i] = "open"
end
end
i += 1
end
a += 1
end
lockers[3] = "open"
lockers.each do |text|
puts text
end
end
lockerproblem