Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
24
Task/Josephus-problem/Lua/josephus-problem.lua
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
function josephus(n, k, m)
|
||||
local positions={}
|
||||
for i=1,n do
|
||||
table.insert(positions, i-1)
|
||||
end
|
||||
local i,j=1,1
|
||||
local s='Execution order: '
|
||||
while #positions>m do
|
||||
if j==k then
|
||||
s=s .. positions[i] .. ', '
|
||||
table.remove(positions, i)
|
||||
i=i-1
|
||||
end
|
||||
i=i+1
|
||||
j=j+1
|
||||
if i>#positions then i=1 end
|
||||
if j>k then j=1 end
|
||||
end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
local s='Survivors: '
|
||||
for _,v in pairs(positions) do s=s .. v .. ', ' end
|
||||
print(s:sub(1,#s-2) .. '.')
|
||||
end
|
||||
josephus(41,3, 1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue