文字配列を入力し、その配列のすべての可能な組み合わせを出力として取得したいとします。例えば 、I入力文字列= 'B、C'、 私はこの形式で出力したい場合:配列内の文字の順列
a b c, a c b, b a c, b c a, c a b, c b a
と同様に、私が取得したいI入力4文字の場合を24種類の組み合わせがあります。これに対してコードを作成しましたが、入力文字の2倍の組み合わせしか返しません。つまり、コードは3文字を入力すると6個の組み合わせが返されますが、4文字を入力すると24個の組み合わせではなく8個の組み合わせしか返されません。用語について
#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