1 function perm(num)
 2     local function _per(num, low)
 3         low = low or 1
 4         local ret = {}
 5         for i=low, math.floor(num/2) do
 6             local t = _per(num-i, i)
 7             for j=1, #t do
 8                 table.insert(ret, {i, table.unpack(t[j])})
 9             end
10         end
11         table.insert( ret, {num} )
12         return ret
13     end
14     return _per(num)
15 end
16 
17 local r = perm(...)
18 for i=1, #r do
19     print(table.unpack(r[i]))
20 end

 最近在做组队匹配的功能,需要根据队伍的总人数找到可以组成一个队伍的所有排列组合,然后组合去寻找合适的房间

 

于是有了这个递归算法

 

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-11-08
  • 2021-07-06
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-15
  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案