【发布时间】: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