|
#include <iostream>
using namespace std;
// 列挙
void perm(int pos, char* str)
{
if(str[pos+1] == '\0')
{
// 処理位置が最後の要素まで来ていたら表示して終了
cout << str << "\n";
return;
}
// 処理位置と右側の要素を入れ替えながら再起呼び出し
for(int i=pos;str[i]!='\0';i++)
{
// 処理位置と右側の要素を入れ替える
swap(str[pos], str[i]);
// 処理位置を増やして再起呼び出し
perm(pos+1,str);
// 元に戻す
swap(str[pos], str[i]);
}
}
int main(void)
{
// 初期値を用意
char str[] = "1234";
// 列挙
perm(0,str);
}