简体   繁体   English

在一定范围内生成一组双精度数的所有排列C ++

[英]Generate all permutations of a set of doubles within a certain range C++

Basically I have n doubles, each of which can have any value within a certain range (the range is different for each value). 基本上,我有n双打,每个双打可以在一定范围内具有任何值(每个值的范围都不同)。 I want a C++ function that can generate all possible permutations of these n values given a certain interval i (this too, can vary for each value). 我想要一个C ++函数,它可以在给定一定的间隔i生成这n值的所有可能排列 (这也可以针对每个值而变化)。

As an example, if n = 2 , i = 0.5 (for both values), min(0) = 0 , max(0) = 1 , min(1) = 0.5 , max(1) = 1.5 , where min(0) is the lower limit for the first value, max(0) is the upper limit for the first value etc., then the function should return the following: 例如,如果n = 2i = 0.5 (两个值), min(0) = 0max(0) = 1min(1) = 0.5max(1) = 1.5 ,其中min(0 )是第一个值的下限,max(0)是第一个值的上限,依此类推,然后函数应返回以下内容:

  - [0.0, 0.5]
  - [0.0, 1.0]
  - [0.0, 1.5]
  - [0.5, 0.5]
  - [0.5, 1.0]
  - [0.5, 1.5]
  - [1.0, 0.5]
  - [1.0, 1.0]
  - [1.0, 1.5]

In my implementation so far, I have the intervals stored in a vector of size n and the ranges for each value stored in a vector of std::pair<double, double> objects, where the first value corresponds to the lower limit and the second value corresponds to the upper limit. 到目前为止,在我的实现中,我将间隔存储在大小为n的向量中,并将每个值的范围存储在std::pair<double, double>对象的向量中,其中第一个值对应于下限,而第二个值对应于上限。 I'm a bit stuck on what to do next though, not very good with recursion unfortunately. 不过,我对下一步的工作有些困惑,遗憾的是递归效果不是很好。 I just need a brief pseudo-code to get things going. 我只需要一个简短的伪代码就可以解决问题。

PS The order is irrelevant. PS顺序无关紧要。

Since you struggle with recursion, avoid it. 由于您在递归方面遇到困难,请避免使用它。 Take a look at the repeating patterns in your output: 看一下输出中的重复模式:

The first column contains the list (0.0, 0.5, 1.0). 第一列包含列表(0.0、0.5、1.0)。 I presume you know how to use a for loop to get these. 我想您知道如何使用for循环来获取这些。

The second column contains repeated blocks of the list (0.5, 1.0, 1.5). 第二列包含列表的重复块(0.5、1.0、1.5)。 Again, I presume you know how to use a for loop for this. 同样,我假设您知道如何为此使用for循环。 There's one such block for each value in the first column. 第一列中的每个值都有一个这样的块。

You just need to think about three lists: The solutions for columns left of the column being processed, the values that the currently processed column should take, and the results you are building. 您只需要考虑三个列表:正在处理的列左侧的列的解决方案,当前已处理的列应采用的值以及您要生成的结果。

For each column, use a nested loop across the columns-left list and the this-column list, placing each combination into the building-output. 对于每一列,请在左列列表和此列列表之间使用嵌套循环,将每个组合放入建筑物输出中。 When you finish one column, move the building-output to the columns-left results and start processing the next column. 当您完成一列时,将建筑物输出移至左列结果,然后开始处理下一列。 Continue until all columns are added. 继续,直到添加了所有列。

No matter how many columns there are, you still only need three loops (one iterates across column number, one across the columns-left results, one across the this-column values). 无论有多少列,您仍然只需要三个循环(一个循环遍历列号,一个遍历左列的结果,一个遍历this-column的值)。 Since the loop nesting depth is fixed, recursion is not needed. 由于循环嵌套深度是固定的,因此不需要递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM