#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
//perm 参考C语言实例解析精粹
//如: 1 2 3 按2位组合列为: 12 13 21 23 31 32
//如: A B C D 按2位组合列为: BA,CA,DA,AB,CB,DB,AC,BC,DC,AD,BD,CD
//J.WANG 2013-6-13 23:17:21
char *p;
int main(){
void perm(int n,char *s);
void permfor(char *s);
int num;
char str[200];
printf("--1--\nplease int a string:\n");
scanf("%s",str);
printf("please int a number:\n");
scanf("%d",&num);
printf("the permuted char are:\n");
p=(char *)malloc(strlen(str));
printf("\n--------per2--------\n");
perm(num,str);
free(p);
printf("\n--------per2--------\n");
permfor(str);
return 0;
}
void permfor(char *s){
int i,j,k;
for(i=0;*(s+i);i++)
for(j=0;*(s+j);j++)
if(i!=j)
for(k=0;*(s+k);k++){
if(j!=k&&i!=k)
printf("%c%c%c\n",*(s+i),*(s+j),*(s+k));
}
}
void perm(int n,char *s){
int i;
char *s1=(char *)malloc(strlen(s));
if(n<1){
printf("%s\n",p);
}else{
strcpy(s1,s);
for(i=0;*(s1+i);i++){
*(p+n-1)=*(s1+i);
*(s1+i)=*s1;
*s1=*(p+n-1);
perm(n-1,s1+1);
}
}
free(s1);
}