Input a string, please print all permutation of it. For example, input”abs”,the output should be”abs”, “asb”, “bas”,”bsa”,”cas”,”csa”.
The recursion version:
#include<iostream>
using namespace std;
void CalAllPermutation(char *perm, int from, int to)
{
if (to <= 1)
{
return;
}
if (from == to)
{
for (int i = 0; i <= to; i++)
{
cout << perm[i];
}
cout << endl;
}
else
{
for (int j = from; j <= to; j++)
{
std::swap(perm[j], perm[from]);
CalAllPermutation(perm, from + 1, to);
std::swap(perm[j], perm[from]);
}
}
}
int main()
{
char arr[] = "abcde";
CalAllPermutation(arr, 0, 4);
return 0;
}