首页 > 代码库 > 猴子选大王

猴子选大王

实现一:

--[[LUA]]-- 猴子选大王:n只猴子围成一圈,报数到m的猴子淘汰,最后剩一只猴子则为大王。local n, m, idx = 12, 3, 0local list = {"1","2","3","4","5","6","7","8","9","10","11","12",}while(n > 1) doidx = (idx + m) % (#list) == 0 and #list or ((idx + m) % (#list))table.remove(list, idx)idx = idx > #list and 0 or (idx - 1)n = n - 1endprint(" >>>>>>>>> ", list[1])

 

猴子选大王