标签:
-----------------------------生产数组的全排列-------------------------------- function permgen(a, n) n = n or #a --默认n为a的大小 if n <= 1 then --printResult(a) coroutine.yield(a) --挂机 else for i = 1, n do a[n], a[i] = a[i], a[n] --将第i个元素放到数组末尾 permgen(a, n-1) --生成其余元素的排列 a[n], a[i] = a[i], a[n] --恢复第i个元素 end end end function printResult(a) for i = 1, #a do io.write(a[i], " ") end io.write("\n") end function permutation(a) local co = coroutine.create(function() permgen(a) end) return function() --迭代器 local code, res = coroutine.resume(co) return res end end for p in permutation({‘a‘, ‘b‘, ‘c‘}) do printResult(p) end
标签:
原文地址:http://www.cnblogs.com/vagaband/p/4242504.html