数组元素的所有排列在C ++中一次采用一定数量的元素

时间:2016-07-15 07:50:34

标签: c++ algorithm math vector

我试图让任何数组的所有排列集一次采用一定数量的元素,例如array = {1,2,3,4}r=3然后可能的排列将是24。这是我使用递归的实现,但这没有给出预期的结果。

void permutationUtil(vector<int> arr, vector<int> data, int start, int end, int index, int r) {
    // Current permutation is ready to be printed, print it
     if (index == r){
    for (int j=0; j<r; j++)
        printf("%d ", data[j]);
    printf("\n");
    return;
}    
    // replace index with all possible elements. The condition
    // "end-i+1 >= r-index" makes sure that including one element
    // at index will make a permutation with remaining elements
    // at remaining positions  
    for (int i = start; i <= end && end - i + 1 >= r - index; i++) {
        data[index] = arr[i];
        permutationUtil(arr, data, i + 1, end, index + 1, r);
    }
}

void printPermutation(vector<int> arr, int n, int r) {

    // A temporary array to store all permutation one by one
    vector<int> data(n);

    // Print all permutation using temprary array 'data[]'
    permutationUtil(arr, data, 0, n - 1, 0, r);
}

3 个答案:

答案 0 :(得分:1)

您可以使用std::next_permutation进行2循环:

void permutationUtilInner(std::vector<int> v,
                          std::function<void (const std::vector<int>&)> f)
{
    do {
        f(v);
    } while (std::next_permutation(v.begin(), v.end()));
}

void permutationUtil(std::vector<int> v,
                     std::size_t r,
                     std::function<void (const std::vector<int>&)> f)
{
    // remainder: range should be sorted for std::next_permutation
    std::vector<bool> b(v.size() - r, false);
    b.resize(v.size(), true);

    std::sort(v.begin(), v.end());
    do {
        std::vector<int> sub;

        for (std::size_t i = 0; i != b.size(); ++i) {
            if (b[i]) {
                sub.push_back(v[i]);
            }
        }
        permutationUtilInner(sub, f);
    } while (std::next_permutation(b.begin(), b.end()));
}

Demo

答案 1 :(得分:0)

这是Delphi的递归实现:

  procedure Arrangement(var A: array of Integer; n, k: Integer; s: string);
  var
    i, t: Integer;
  begin
    if k = 0 then
      Output(s)
    else
      for i := 0 to n - 1 do begin
        t := A[i];
        A[i] := A[n - 1];  //store used item in the tail
        Arrangement(A, n - 1, k - 1, s + IntToStr(t) + ' ');  //recursion without tail
        A[i] := t;  //get it back
      end;
  end;

答案 2 :(得分:0)

您的算法不完整。它总是在增加。像2,1,3这样需要不增加序列的情况不包括在内。

在第二个for循环中,将int i=start更改为int i=0以解决问题。