例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串
abc、acb、bac、bca、cab和cba。
分析:这是一道很好的考查对递归理解的编程题,
因此在过去一年中频繁出现在各大公司的面试、笔试题中。
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
void Perm(char *array,int pos,int last)
{
if(pos == last)
{
for(int i = 0;i<=pos;i++)
cout <<array[i];
cout <<endl;
}
else
{
for(int i = pos ;i <=last;i++)
{
swap(array[i],array[pos]);
Perm(array,pos+1,last);
swap(array[pos],array[i]);
}
}
}
int main()
{
char a[] = {'a','b','c'};
Perm(a,0,2);
return 0;
}