Nested for loops - how to ignore certain combinations?

前端 未结 2 2229
暗喜
暗喜 2021-02-10 20:58

I\'m doing some kind of brute force attack to solve a problem. Theoratically it is a working solution, it also really is, but it takes rather long.

I have 7 nested for l

2条回答
  •  猫巷女王i
    2021-02-10 21:41

    The problem is that you are doing extra work. In your c loop you already know that a and b are not equal, but you check anyway. You could exploit the flexibility of c-style for loops:

    for(var a=0;a<14;a++) {
        for(var b=0;b<14 && b!=a;b++) {
            for(var c=0;c<14 && c!=a && c!=b;c++) {
                ...
            }
        }
    }
    

    This gets a little verbose in the inner loop, but is at least simple.

提交回复
热议问题