// 求给定字符串的组合情况(注意是组合,不是排列)
// 思路:可以考虑组合字符串中只有1个字符、只有2个字符、只有3个字符、、、、、、只有n-1个字符
// 定义组合字符串中有m个字符(n>m>=0 0代表1个字符) 则这m个字符中的第一个可以从原字符数组中
// 的第0个开始取,则后面的m-1个字符则必须从0+1个开始取,直到取到的字符长度等于m为止
// 同理如果第0个字符从i开始取,则后面的m-1个字符必须从第i+1开始取
// 注意:由于是组合情况,所有后面的字符没必要再取前面的已经取到过的字符,比如 abc组合时 如果第一个
// 字符已经取到b,则第二个字符只需要取b后面的字符即c,不能再取字符a了 因为ab 和 ba是同一种组合情况
#include<stdio.h>
#include<string.h>
// 定义数组长度
#define LEN 20
//定义原数组
char data[LEN];
//定义结果数组
char desData[LEN];
//原数组长度
int len;
// 统计总共有多少种组合
int count;
// 输出所有定长字符串组合
void PrintAllStrList(int index,int totleLen,int desIndex);
int main()
{
while(true)
{
scanf("%s",data);
len = strlen(data);
count=0;
for(int i=0;i<len;i++)
{
PrintAllStrList(0,i,0);
}
printf("总计:%d中组合\n",count);
}
return 0;
}
/// index:代表从原数组中哪一个开始取,
/// totleLen:代表本次组合需要取到的字符串的长度
/// desIndex: 代表目标数组中已存存了desIndex个数
void PrintAllStrList(int index,int totleLen,int desIndex)
{
if(index>=len || index<0 || totleLen>=len || totleLen<0 || desIndex<0 || desIndex>totleLen)
return;
for(int i=index;i<len;i++)
{
desData[desIndex]=data[i];
if(desIndex==totleLen)
{
desData[desIndex+1]='\0';
++count;
printf("%s\n",desData);
}
else
{
PrintAllStrList(i+1,totleLen,desIndex+1);
}
}
}
// 思路:可以考虑组合字符串中只有1个字符、只有2个字符、只有3个字符、、、、、、只有n-1个字符
// 定义组合字符串中有m个字符(n>m>=0 0代表1个字符) 则这m个字符中的第一个可以从原字符数组中
// 的第0个开始取,则后面的m-1个字符则必须从0+1个开始取,直到取到的字符长度等于m为止
// 同理如果第0个字符从i开始取,则后面的m-1个字符必须从第i+1开始取
// 注意:由于是组合情况,所有后面的字符没必要再取前面的已经取到过的字符,比如 abc组合时 如果第一个
// 字符已经取到b,则第二个字符只需要取b后面的字符即c,不能再取字符a了 因为ab 和 ba是同一种组合情况
#include<stdio.h>
#include<string.h>
// 定义数组长度
#define LEN 20
//定义原数组
char data[LEN];
//定义结果数组
char desData[LEN];
//原数组长度
int len;
// 统计总共有多少种组合
int count;
// 输出所有定长字符串组合
void PrintAllStrList(int index,int totleLen,int desIndex);
int main()
{
while(true)
{
scanf("%s",data);
len = strlen(data);
count=0;
for(int i=0;i<len;i++)
{
PrintAllStrList(0,i,0);
}
printf("总计:%d中组合\n",count);
}
return 0;
}
/// index:代表从原数组中哪一个开始取,
/// totleLen:代表本次组合需要取到的字符串的长度
/// desIndex: 代表目标数组中已存存了desIndex个数
void PrintAllStrList(int index,int totleLen,int desIndex)
{
if(index>=len || index<0 || totleLen>=len || totleLen<0 || desIndex<0 || desIndex>totleLen)
return;
for(int i=index;i<len;i++)
{
desData[desIndex]=data[i];
if(desIndex==totleLen)
{
desData[desIndex+1]='\0';
++count;
printf("%s\n",desData);
}
else
{
PrintAllStrList(i+1,totleLen,desIndex+1);
}
}
}