c++ : dynamic number of nested for loops (without recursion)

后端 未结 3 723
野趣味
野趣味 2020-12-08 11:56

I\'m writing a code segment that iterates through every permutation of n digits. So for example, if n = 3, I would want to iterate through each of the following elements:

3条回答
  •  一生所求
    2020-12-08 12:13

    If you want the permutation for all the digits for a specific length;as you have shown example of 3 digits. Instead of running 3 nested loops, run a single loop of 10^3 which will give you all the permutations.

    Split the number obtained into digits in each iteration if you want to use it for indexing.

    Thus you will be needing just one loop rather than nested loops.

提交回复
热议问题