字符的递归排列生成器
recursive permutation generator for characters
可能的重复项:
生成具有所有字符排列的字符串
我是 c++ 的初学者,我真的需要你的帮助。我正在做使用递归进行排列的程序。这是我的代码,但输出很奇怪,有相同的数字重复很多次和空格。我无法找出问题所在,或者可能需要添加更多内容。请帮助我。这是我的代码:
#include <iostream>
using namespace std;
#define swap(x,y,t) ((t)=(x), (x)=(y), (y)=(t))
void perm(char *list, int i, int n);
int main(){
char a[4]={'a','b','c'};
perm(a,0,3);
//cout<<a<<endl;
return 0;
}
void perm(char *list, int i, int n){
int j, temp;
if (i==n){
for (j=0; j<=n; j++)
printf("%c", list[j]);
printf(" ");
}
else {
for (j=i; j<=n; j++){
swap(list[i],list[j],temp);
perm(list,i+1,n);
swap(list[i],list[j],temp);
cout<<list<<endl;
}
}
}
该函数是正确的,但您没有正确调用它。
perm(a,0,3);
应该是
perm(a,0,2);
为什么?
您的 for 循环:
for (j=i; j<=n; j++){
直到n
,所以n
应该是一个有效的索引。
工作正常
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 递归无序映射
- TSP递归解的迭代形式
- 问题 - 递归函数以返回文本文件排列
- 字符的递归排列生成器
- 使用递归的括号有效排列的输出差异
- 正十进制整数的递归函数位数,按c++的相反顺序排列
- 非递归二进制字符串排列生成器
- 计算在不进行排列的情况下递归相加为N的方法的数量
- c++中生成排列中的递归问题
- 尝试从以下递归排列函数中产生不同的结果
- 递归排列,Ellis Horowitz算法和数据结构混淆
- 使用向量的递归排列