例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。
/*
* Copyright (c) 2011 alexingcool. All Rights Reserved.
*/
#include <iostream>
#include <algorithm>
using namespace std;
char array[] = {'a', 'b', 'c'};
const int size = sizeof array / sizeof *array;
void Perm(char *array, int pos, int last)
{
if (pos == last) {
copy(array, array + size, ostream_iterator<char>(cout, ""));
cout << endl;
}
else {
for(int i = pos; i <= last; i++) {
swap(array[i], array[pos]);
Perm(array, pos + 1, last);
swap(array[i], array[pos]);
}
}
}
void main()
{
Perm(array, 0, 2);
}
来源: 点击打开链接