c语言next用法,尝试使用next_permutation模拟C中的python组合

我需要将Python编写的代码片段移植到C

但是该代码段正在使用python中itertools的组合.

我真的很想移植到C的这一行是:

for k in combinations(range(n-i),2*i):

Python中的range(n-i)将生成一个从0到(n-i)-1的列表

令n = 16,i = 5

打印范围(n-i)

输出:

[0,1,2,3,4,5,6,7,8,9,10]

和python组合将在该列表中生成所有可能的组合.

例如

打印列表(组合(范围(n-i),2 * i))

输出:

[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),

(0, 1, 2, 3, 4, 5, 6, 7, 8, 10),

(0, 1, 2, 3, 4, 5, 6, 7, 9, 10),

(0, 1, 2, 3, 4, 5, 6, 8, 9, 10),

(0, 1, 2, 3, 4, 5, 7, 8, 9, 10),

(0, 1, 2, 3, 4, 6, 7, 8, 9, 10),

(0, 1, 2, 3, 5, 6, 7, 8, 9, 10),

(0, 1, 2, 4, 5, 6, 7, 8, 9, 10),

(0, 1, 3, 4, 5, 6, 7, 8, 9, 10),

(0, 2, 3, 4, 5, 6, 7, 8, 9, 10),

(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]

我想使用std :: vector和C中的next_permutation生成类似的输出,但是我仍然得到错误的结果.这是我目前的方法:

for(int j = 0; j < n-i; j++) {

temp_vector.push_back(j);

}

该代码段等效于Python中的range(n-i).

但是以下代码段:

do {

myvector.push_back(temp_vector);

} while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));

cout<

不等同于Python中的组合(range(n-i),2 * i)),我已经尝试了许多变体,但仍然无法得出我期望的结果.

例如:

设n = 16

我= 5

Python

>>>打印len(list(combinations(range(n-i),2 * i)))

11

C

#include

#include

using namespace std;

int main() {

vector temp_vector;

vector< vector > myvector;

int n = 16, i = 5;

for(int j = 0; j < n - i; j++) {

temp_vector.push_back(j);

}

do {

myvector.push_back(temp_vector);

} while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));

cout<

return 0;

}

g groups.cpp

./a.out

3628800

任何指导将不胜感激!非常感谢!

THE END
< <上一篇
下一篇>>