打印字符串的排列

#include <iostream>
#include <string.h>

void swap(char* a, char* b)
{
    char tmp = *a;
    *a = *b;
    *b = tmp;
}

void permutation(char* a, int i, int j)
{
    if ( i == j)
        std::cout << a << '\n';
    else
    {
        for(int m = i; m < j; ++m)
        {
            swap(a+i, a+m);
            permutation( a, i+1, j);
            swap(a+i, a+m);
        }
    }
}

int main()
{
    char test[100];
    strcpy(test, "abc");
    permutation(test, 0, strlen(test));
}

你可能感兴趣的:(打印字符串的排列)