是否有像next_permutation这样的函数,但是对于重复的排列?

Eri*_*ous 4 c++ arrays combinations permutation repeat

我想要做的是找到重复其内容的一维数组的每个排列.

例如

int array[]={1,2,3};
for(i=0;i<3;i++){
    next_permutation(array,array+3)
    for(int j=0;j<=3;j++){
        printf("%d ",array[j]);
    }
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)

将返回:

1 2 3
1 3 2
2 1 3
etc...
Run Code Online (Sandbox Code Playgroud)

我希望函数返回的内容:

1 1 1
1 1 2
1 2 1
2 1 1
1 2 2
2 2 1
2 1 2
1 1 3
1 3 1
3 1 1
etc...
Run Code Online (Sandbox Code Playgroud)

有功能可以做到吗?

提前谢谢,Erik

Zan*_*Jie 6

你不是在进行排列而是在计算.

防爆.如果你的枚举集{0,1}超过3位数,你会得到:

000
001
010
011
100
101
110
111
Run Code Online (Sandbox Code Playgroud)

看,这只是二进制计数.

因此,将您的元素集映射为n位数,然后基于n的计数将为您提供正确的awnser