我输入一个字符数组,并希望获得该数组的所有可能组合作为输出。 例如,如果我输入字符数组=“A,B,C”, 我想在这种形式的输出:数组中字符的排列
a b c, a c b, b a c, b c a, c a b, c b a
并且类似地,如果我输入4个字符,我想要得到24个组合。我为此做了一个代码,但它返回的组合只是输入字符数量的两倍。也就是说,如果我输入3个字符(这是正确的),代码将返回6个组合,但是如果我输入4个字符,它将只返回8个可能的组合,而不是24个组合。 我的代码如下:
#include <iostream>
#include<string.h>
#include<stdio.h>
using std::cout;
void getCombination(char *);
int main()
{
const int maxStringSize = 26;
char thisString[maxStringSize];
cout<<"Enter String = ";
gets (thisString);
getCombination(thisString);
return 0;
}
void getCombination(char *thisString)
{
int stringSize=strlen(thisString);
for(int i = 0; i<stringSize; i++)
{
for(int j = 0; j<stringSize; j++)
{
cout<<thisString[(i+j)%stringSize];
}
cout<<"\n";
for(int k = stringSize-1; k>=0; k--)
{
cout<<thisString[(i+k)%stringSize];
}
cout<<"\n";
}
}
[在一个数组获取所有排列]的可能重复(http://stackoverflow.com/questions/1272828/getting-all-the-permutations-in-an-array) –
@Bo Persson的:可能的:但是不同的语言。 – Johnsyweb