一个全排列函数:如p([1,2,3]) ,输出: [123],[132],[213],[231],[321],[323]。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void full_sorting(char strin[]){
int len = strlen(strin);
char *isuse = (char *)calloc(len, sizeof(char)); //现在已经使用的字符集
int *usexb = (int *)calloc(len, sizeof(int)); //该位置使用的是strin中字符的下标个数
int use = 0, i, loc = 1;
while(1){
if(usexb[use ] )
isuse[usexb[use] -1] = 0;
usexb[use] = loc;
isuse[loc -1] = 1;
use ++;
if(use == len ){
for(i = 0 ; i < len; i++)
printf("%c", strin[usexb[i]-1]);
printf("\n");
isuse[usexb[len -1] -1] = 0;
usexb[len - 1] = 0;
use -= 2;
}
aa: if(usexb[use])loc = usexb[use] - 1 ; else loc = 0;
while(isuse[loc] && loc < len) loc ++;
if(loc >= len){
isuse[usexb[use] - 1] = 0;
usexb[use] = 0;
use --;
if(0 > use) return;
goto aa;
}
loc ++;
}
}
int main(int argc, char *argv[]){
char strin[255] ;
scanf("%s", strin);
full_sorting(strin);
}