字符串的全排列

1.字符串全排列

#include<stdio.h>
#include<stdlib.h>
#define swap(t,x,y) {t = (x);x = (y);y = (t);}
void CalcAllPermutation(char *perm,int from,int to)
{
    if (to < 0)
        return ;
    int i = 0;
    char t;
    if (from == to)
    {
        for (i = 0;i <= to;i++)
           printf("%c",perm[i]);
        printf("\n");
    }
    else
    {
        for (i = from;i <= to;i++)
        {
            swap(t,perm[i],perm[from]);//将from-to中的一个元素固定在第一位
            CalcAllPermutation(perm,from+1,to);
            swap(t,perm[i],perm[from]);//恢复以前的状态
        }
    }
}
int main()
{
    char s[] = "abcd";
    CalcAllPermutation(s,0,3);
    return 0;
}

 

posted @ 2017-09-05 11:43  Allen101  阅读(108)  评论(0)    收藏  举报