【问题标题】:Generating all R-digit numbers among N digits in C++ (combinations, iterative)?在 C++ 中的 N 位中生成所有 R 位数字(组合,迭代)?
【发布时间】:2018-02-27 14:18:39
【问题描述】:

我有一个程序,我必须在 C++ 中生成 N 位数字中的所有 R 位数字,例如 N=3(从 1 到 N 的所有数字)和 R=2 程序应该生成 12 13 21 23 31 32 .我尝试如下使用数组来执行此操作,但它似乎无法正常工作。

#define nmax 20
#include <iostream>
using namespace std;
int n, r;
void print(int[]);

int main()
{
    cin >> n;
    cin >> r;

    int a[nmax];
    int b[nmax];
    int used[nmax];

    for (int p = 1; p <= n; p++) {
        //Filling the a[] array with numbers from 1 to n
        a[p] = n;
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < r; j++) {
            b[j] = a[i];
            used[j] = 1;
            if (used[j]) {
                b[j] = a[i + 1];
            }
            used[j] = 0;
        }
        print(b);
    }

    return 0;
}

void print(int k[]) {
    for (int i = 0; i < r; i++) {
        cout << k[i];
    }
}

【问题讨论】:

  • 我想问我做错了什么?这是一个相当广泛的问题,会引起很多意见。如果我们专注于“它似乎无法正常工作”,它似乎做错了什么?
  • 什么是“R-Digit”?
  • 你到底为什么要这么做iteratively
  • 请不要从您的问题中删除您的代码。你这样会降低问题的质量。

标签: c++ iteration combinations generate


【解决方案1】:

如果我正确理解您的问题,您可以探索this website,它解释了问题并彻底提出了解决方案。

这是一个稍微改动的代码:

请注意,对于较大的 N 值,时间是一个问题。

#define N    5   // number of elements to permute.  Let N > 2
#include <iostream>
using namespace std;

// NOTICE:  Original Copyright 1991-2010, Phillip Paul Fuchs

void PrintPerm(unsigned int *a, unsigned int j, unsigned int i){
   for(unsigned int x = 0; x < N; x++)
      cout << " " << a[x];
   cout << "    swapped( " << j << " , " << i << " )\n";
}

void QuickPerm(void){
   unsigned int a[N], p[N+1];
   register unsigned int i, j, PermCounter = 1; // Upper Index i; Lower Index j

   for(i = 0; i < N; i++){   // initialize arrays; a[N] can be any type
      a[i] = i + 1;   // a[i] value is not revealed and can be arbitrary
      p[i] = i;
   }
   p[N] = N; // p[N] > 0 controls iteration and the index boundary for i
   PrintPerm(a, 0, 0);   // remove comment to PrintPerm array a[]
   i = 1;   // setup first swap points to be 1 and 0 respectively (i & j)
   while(i < N){
      p[i]--;             // decrease index "weight" for i by one
      j = i % 2 * p[i];   // IF i is odd then j = p[i] otherwise j = 0
      swap(a[i], a[j]);   // swap(a[j], a[i])
      PrintPerm(a, j, i);   // remove comment to PrintPerm target array a[]
      PermCounter++;
      i = 1;              // reset index i to 1 (assumed)
      while (!p[i]) {      // while (p[i] == 0)
         p[i] = i;        // reset p[i] zero value
         i++;             // set new index value for i (increase by one)
      } // while(!p[i])
   } // while(i < N)
    cout << "\n\n ---> " << PermCounter << " permutations. \n\n\n";
} // QuickPerm()

int main(){
    QuickPerm();
} //main

这里是原始代码中修改项目的列表。

  1. N 定义为 5 而不是 12。
  2. 已添加计数器以获得更多信息结果。
  3. 使用 c++ 标准库的 swap() 函数减少了原始交换指令。
  4. getch() 已被删除。
  5. “Display()”函数已重命名为“PrintPerm()”。
  6. printf() 函数已替换为 cout
  7. 已添加排列的打印次数。

【讨论】:

  • @JHBonarius:我看不到 OP 在哪里要求 OO。这说明了核心问题。万一需要,做OO应该不成问题。
猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
相关资源
最近更新 更多