转换为非递归 [英] Convert to Non-recursive
本文介绍了转换为非递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码是递归组合。
所以我想转换为非递归的。
plz..help me ..
我的代码是..
this code is recursive combination.
so i wanna convert to Non-recursive one.
plz..help me..
my code is..
#include <iostream>
using namespace std;
void comb(int n, int r, int *arr, int sz) {
for (int i = n; i >= r; i --) {
// choose the first element
arr[r - 1] = i;
if (r > 1) { // if still needs to choose
// recursive into smaller problem
comb(i - 1, r - 1, arr, sz);
} else {
// print out one solution
for (int i = 0; i < sz; i ++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
}
int main() {
const int N = 4;
int *arr = new int[3];
for(int i=1;i<=N;i++)
comb(N, i, arr, i);
return 0;
}
*Result
1
2
3
4
1 3
1 2
1 4
2 3
2 4
3 4
1 2 3
1 2 4
1 3 4
2 3 4
1 2 3 4
推荐答案
这是所有家庭作业问题的通用帮助 - 不仅是这个问题,还有任何计算问题。
- 确定问题所在
This is universal help for all of your homework problems - not only for this problem but any computing problem.
- Determine what the problem is
- 确定你会用铅笔和纸来解决什么步骤
- 确定哪些部分是重复的
这篇关于转换为非递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文