已知字符串的字符互不相同,输出他们的任意序列。比如 字符串是"ab"输出 “aa”,“ab”,“ba”,“bb”
第一个字符 | 第二个字符 | 结果 |
---|
| a | aa |
a | | |
| b | ab |
| | |
| a | ba |
b | | |
| b | bb |
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void CalcAllPermutation(char * str, char * buff, int index, int len){
int strLen = strlen(str);
if(index >= strLen){
return;
}
if(len == strLen){
printf("%s\n",buff);
return;
}else{
for(int i = 0;i <= strLen;i++){
buff[len] = str[i];
CalcAllPermutation(str,buff,i,len+1);
}
}
}
int main(int argc, char * argv[]){
char str[] = "abc";
char * buff = (char *)calloc(1,sizeof(char) * strlen(str)+1);
printf("递归所有组合:\n");
CalcAllPermutation(str,buff,0,0);
return 0;
}