/*偶然看到的一个算法题,关于全排列的,用递归代码挺简洁的,记下*/
#include <iostream>
using namespace std;
void permutation(char* src, char* beg){
if(!src || !beg){
cerr<<"error!\n";
return;
}
if(*beg == '\0'){
cout<<src<<endl;
}else{
for(char *ch = beg; *ch != '\0'; ++ch){
char tmp = *ch;
*ch = *beg;
*beg = tmp;
permutation(src, beg+1);
//restore
*beg = *ch;
*ch = tmp;
}
}
}
int main(int argc, char* argv[]){
char src[] = "123456";
permutation(src, src);
return 0;
}
打印字符串的全排列
最新推荐文章于 2022-05-09 23:32:44 发布