生成所有可能的组合在0 1 n 1 n k个数中。每个组合都应该按升序[重复]

时间:2021-12-25 21:46:49

This question already has an answer here:

这个问题已经有了答案:

Please understand that this is not a duplicate question. This question needs sorted combinations. Please read the question before. The combination can have repeats of a number. Currently, i have tried generating permutations of n-k+1 0s and k 1s. But it does not produce the combinations with repeats. For example: Choosing 3 numbers from 0, 1,....n, it generates 9 combinations:

请理解这不是一个重复的问题。这个问题需要排序的组合。请先读问题。组合可以有一个数字的重复。目前,我已经尝试生成n-k+1 0和k 1的排列。但是它不会产生重复的组合。例如:选择3数字从0,1,....n,产生9种组合:

(0 1 2),
(0 1 3),
(0 1 4),
(0 2 3),
(0 3 4),
(1 2 3),
(1 2 4),
(1 3 4),
(2 3 4)

I need it include these combinations too:

我需要它也包括这些组合:

(0, 0, 0),
(0, 0, 1),
(0, 0, 2),
(0, 0, 3),
(0, 0, 4),
(0, 1, 1),
(0, 2, 2),
(0, 3, 3),
(0, 4, 4),
(1, 1, 1),
(1, 1, 2),
(1, 1, 3),
(1, 1, 4),
(1, 2, 2),
(1, 3, 3),
(1, 4, 4),
(2, 2, 2),
(2, 2, 3),
(2, 2, 4),
(2, 3, 3),
(2, 4, 4),
(3, 3, 3),
(3, 3, 4),
(3, 4, 4),
(4, 4, 4)

What's the most efficient way to get this result? I have used next_permutation to generate the combination right now. Take a look please:

得到这个结果最有效的方法是什么?我已经使用next_permutation生成这个组合。请看看:

    vector<ll> nums, tmp;
    for(i = 0; i <= m - n; i++)
    {
        nums.push_back(0);
    }
    for(i = 0; i < n; i++)
    {
        nums.push_back(1);
    }
    do 
    {
        tmp.clear();
        for(i = 0; i <= m; i++)
        {
            if(nums[i] == 1)
            {
                tmp.push_back(i);
            }
        }
        for(i = 0; i < tmp.size(); i++)
        {
            cout << tmp[i] << " ";
        }
        cout << endl;
    } while(next_permutation(nums.begin(), nums.end()));

2 个解决方案

#1


2  

Your 'combinations' are essentially k-digit numbers in base-N numeral system. There are N^k such numbers.

你的“组合”本质上是基数n的数字系统中的k位数。有N ^ k这样的数字。

The simplest method to generate them is recursive.

生成它们的最简单方法是递归的。

You can also organize simple for-cycle in range 0..N^k-1 and represent cycle counter in the mentioned system. Pseudocode

您还可以在0范围内组织简单的for-cycle。N ^ k - 1代表提到的系统循环计数器。伪代码

for (i=0; i<N^k; i++)  {  //N^k is Power, not xor
   t = i 
   d = 0
   digit = {0}
   while t > 0 do {
      digit[d++] = t%N //modulus
      t = t / N    //integer division
   }
   output digit array
}

#2


0  

Following may help:

可以帮助:

bool increment(std::vector<int>& v, int maxSize)
{
    for (auto it = v.rbegin(); it != v.rend(); ++it) {
        ++*it;
        if (*it != maxSize) {
            return true;
        }
        *it = 0;
    }
    return false;
}

Usage:

用法:

std::vector<int> v(3);

do {
    // Do stuff with v
} while (increment(v, 10));

Live demo

现场演示

#1


2  

Your 'combinations' are essentially k-digit numbers in base-N numeral system. There are N^k such numbers.

你的“组合”本质上是基数n的数字系统中的k位数。有N ^ k这样的数字。

The simplest method to generate them is recursive.

生成它们的最简单方法是递归的。

You can also organize simple for-cycle in range 0..N^k-1 and represent cycle counter in the mentioned system. Pseudocode

您还可以在0范围内组织简单的for-cycle。N ^ k - 1代表提到的系统循环计数器。伪代码

for (i=0; i<N^k; i++)  {  //N^k is Power, not xor
   t = i 
   d = 0
   digit = {0}
   while t > 0 do {
      digit[d++] = t%N //modulus
      t = t / N    //integer division
   }
   output digit array
}

#2


0  

Following may help:

可以帮助:

bool increment(std::vector<int>& v, int maxSize)
{
    for (auto it = v.rbegin(); it != v.rend(); ++it) {
        ++*it;
        if (*it != maxSize) {
            return true;
        }
        *it = 0;
    }
    return false;
}

Usage:

用法:

std::vector<int> v(3);

do {
    // Do stuff with v
} while (increment(v, 10));

Live demo

现场演示