プログラム設計とアルゴリズム(二)全配列
8754 ワード
テーマは異なる小文字からなる文字列を与え,この文字列のすべての全配列を出力する.小文字には「a」<「b」<......<「y」<「z」があり、与えられた文字列のアルファベットは小さい順に並べられていると仮定する.
入力入力は1行のみで、異なる小文字からなる文字列で、既知の文字列の長さは1~6です.
この文字列のすべての配列を出力し、行ごとに配列します.アルファベット順の比較的小さい配列が前に必要です.アルファベット順は次のように定義されます.
S=s 1 s 2…sk,T=t 1 t 2…tkが知られていると、SSTLのnext_に直接permutation()関数はOKです.簡単できれいなコードを参考にして、この関数をよく紹介するリンクSTLのnext_を置きます.permutation()関数
コード#コード#
公式コード
入力入力は1行のみで、異なる小文字からなる文字列で、既知の文字列の長さは1~6です.
この文字列のすべての配列を出力し、行ごとに配列します.アルファベット順の比較的小さい配列が前に必要です.アルファベット順は次のように定義されます.
S=s 1 s 2…sk,T=t 1 t 2…tkが知られていると、S
コード#コード#
#include
#include
#include
#include
using namespace std;
void oj_2_1(){
char a[10];
cin>>a;
int len=strlen(a);
do{
cout<<a<<endl;
}while(next_permutation(a,a+len));
}
int main(){
oj_2_1();
return 0;
}
公式コード
#include
#include
#include
#include
using namespace std;
const int M = 8;
char str[M];
char permutation[M];
bool used[M] = { 0 };
int L = 0;
void Permutation(int n)
{
if (n == L) {
permutation[L] = 0;
cout << permutation << endl;
return;
}
for (int i = 0; i < L; ++i) {
if (!used[i]) {
used[i] = true;
permutation[n] = str[i];
Permutation(n + 1);
used[i] = false;
}
}
}
int main()
{
cin >> str;
L = strlen(str);
sort(str, str + L);
Permutation(0);
return 0;
}