废话不多说,直接上代码
#include <stdio.h> #define swap(a,b){long temp=*a;*a=*b;*b=temp;} void Permutation(char* pStr, char* pBegin) { if(!pStr || !pBegin) return; if(*pBegin == 0) { printf("%s\n",pStr); } else { for(char* pCh = pBegin; *pCh != ‘\0‘; pCh++) { // swap pCh and pBegin swap(pCh,pBegin); //printf("--%s-----%s\n",pCh,pBegin); Permutation(pStr, pBegin + 1); // restore pCh and pBegin swap(pCh,pBegin); //printf("++%s-----%s\n",pCh,pBegin); } } } void main(){ char str[] ={‘a‘,‘b‘,‘c‘,‘\0‘}; char *p=str; Permutation(p, p); }
编译运行
[root@chenpingzhao code]# gcc -o cs cs.c -std=c99 [root@chenpingzhao code]# ./cs abc acb bac bca cba cab
原文:http://www.cnblogs.com/chenpingzhao/p/4572738.html