题目:输入一个字符串,输出该字符串中字符的所有组合。
举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。
这里提供一种新思路。算法的思想就是用整数的二进制表示字符串排列中是否有字符,1表示有该字符,0表示没有。
代码如下:
char str[] = "abcd";
int length = strlen(str);
int k = (1<<length) - 1 ;
while (k)
{
int j = k;
int count = 0;
while(j>0)
{
int temp = j&1;
count++;
if (temp>0)
{
cout<<str[length-count];
}
j = j>>1;
}
cout<<endl;
k--;
}
7476

被折叠的 条评论
为什么被折叠?



