【问题标题】:All Permutation of an array elements taken certain number of element at a time in C++在 C++ 中,数组元素的所有排列一次取一定数量的元素
【发布时间】:2016-07-15 07:50:34
【问题描述】:

我试图让任何数组的所有排列组合一次获取一定数量的元素,例如如果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);
}

【问题讨论】:

  • 您在寻找std::next_permutation 吗?
  • 数组可能包含重复项吗?
  • @Arunmu std::next_permuation 对整个数组执行排列,一次取所有元素
  • @Jarod42 复制是不允许的。
  • @AshutoshPandey std::next_permutation 将迭代器作为输入。所以,应该由你决定。

标签: c++ algorithm math vector


【解决方案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

【讨论】:

  • 谢谢..它完成了这项工作。
【解决方案2】:

这是 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;

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2021-11-19
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多