编写一个循环,当尝试了每个排列时结束。

我有一个函数,它以六个八进制(0-7)数字作为参数,并返回 true 或 false。

我想运行一个循环,尝试 0-7 的每个值的所有排列,并计算“true”返回的数量。

类似于:

function count_possibles()
local count=0
local a,b,c,d,e,f=0,0,0,0,0,0
while possiblepermutations > 0 do
    if compare(a,b,c,d,e,f) == true then count = count +1 end
    permute(a,b,c,d,e,f)
    possiblepermutations = possiblepermutations -1
return count
end

我尝试了一些在 http://www.lua.org/pil/9.3.html 提供的示例,但这些示例都是关于迭代表格的,不太适合我正在做的事情。

我不一定关心性能,此函数用于测试我编写的 compare 函数。

有没有一种简单的方法可以循环某些内容,直到尝试了所有可能的排列?

点赞
用户2226988
用户2226988

直接的方法看起来很好,符合所述需求:

local count = 0
local total = 0
for a = 0, 7 do
    for b = 0, 7 do
        for c = 0, 7 do
            for d = 0, 7 do
                for e = 0, 7 do
                    for f = 0, 7 do
                        total = total + 1
                        if compare(a,b,c,d,e,f) == true then count = count +1 end
                    end
                end
            end
        end
    end
end
return count, total

当然,这与排列无关。我更倾向于冲突的要求(如问询者的代码所示),即前面的参数是0,0,0,0,0,0。

2014-01-09 18:06:58