问题描述:输入一个字符串,输出它的所有组合。例如,输入abc,则输出 a b c ab ac bc abc。
分析:如果输入的字符串的长度为n,则这个字符串能构成长度为1的组合、长度为2的组合、……、长度为n的组合。在求n个字符的长度为m(1<=m<=n)的组合的时候,我们可以把这n个字符分成两部分:第一个字符和其余所有字符。如果把第一个字符放进组合中,则下一步求在剩余的n-1个字符中长度为m-1的组合;如果不把第一个字符放进组合中,则下一步求在剩余的n-1个字符中长度为m的组合。这是典型的递归形式。下面是参考代码。
/* --- 注释版 --- */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Combination(char *pStr, int length);
void CombinationCore(char *pStr, char *pResult, char *pch, int num, int length);
void Combination(char *pStr, int length)
{
//空指针异常判断
if(pStr==NULL)
return;
//申请保存结果的地址空间
char *pResult = (char *)malloc((length+1)*sizeof(char));
//申请失败
if(pResult == NULL)
{
printf("alloc memory for pResult fail...\n");
return;
}
//申请成功
else
{
printf("alloc memory for pResult success...\n");
//创建用于跟踪的哨兵
char *pCh = pResult;
int num;
//循环打印出各种数目的组合情况
for(num=1; num<=length; num++)
{ printf("\n%d个字符的组合\n", num);
CombinationCore(pStr, pResult, pCh, num, length);
}
}
}
/*各个参数的介绍
* char *pStr :源字符串的当前指针
* char *pResult :保存结果字符串的首地址,在每次递归中它是不动的,用于最终打印结果
* char *pCh :下一个待放进结果的字符指针
* int num :结果字符串剩余长度
* int length :源字符串中剩余长度
*/
void CombinationCore(char *pStr, char *pResult, char *pCh, int num, int length)
{
//递归终止条件,源字符串中剩余长度 < 结果字符串剩余长度,直接返回
if(length < num)
return;
//递归终止条件,源字符串中剩余长度为零
if(num == 0)
{
*pCh = '\0';
printf("%s\n", pResult);
return;
}
//递归终止条件,源字符串中剩余长度 = 结果字符串剩余长度
if(num == length)
{
strcpy(pCh, pStr);
printf("%s\n", pResult);
return;
}
//假设当前结果字符串包含当前源字符串的第一个字符
*pCh = *pStr;
CombinationCore(pStr+1, pResult, pCh+1, num-1, length-1);
//不包含当前源字符串的第一个字符
CombinationCore(pStr+1, pResult, pCh, num, length-1);
}
int main()
{
char a[] = "abc";
Combination(a, 3);
getch();
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void Combination(char* pStr, int length);
void CombinationCore(char* pBegin, char* pResult, char* pCh, int num, int length);
void Combination(char* pStr, int length){
if(pStr == NULL || length <= 0)
return;
char* pResult = (char *)malloc((length + 1) * sizeof(char));
char* pCh = pResult;
for(int num = 1; num <= length; num++)
CombinationCore(pStr, pResult, pCh, num, length);
}
void CombinationCore(char* pBegin, char* pResult, char* pCh, int num, int length){
if(num == 0){
*pCh = '\0';
printf("%s\n", pResult);
}
else if(num == length){
strcpy(pCh, pBegin);
printf("%s\n", pResult);
}
else{
*pCh = *pBegin;
CombinationCore(pBegin + 1, pResult, pCh + 1, num - 1, length - 1);
CombinationCore(pBegin + 1, pResult, pCh, num, length - 1);
}
}
int main(){
char string[] = "abcd";
Combination(string, 4);
return 0;
}