#include <stdio.h> #include <string.h> void combine(char *src, char *out,int rec, int start, int end) { int i; if(start==end) return; for(i=start;i<end;i++){ out[rec]=src[i]; out[rec+1]='\0'; printf("%s\n",out); combine(src,out,rec+1,i+1,end); } } int main(int argc, char *argv[]) { int len=strlen(argv[1]); char out[len]; combine(argv[1],out,0,0,len); return 0; }
/** *从字符串中取出n个的组合 */ #include <stdio.h> #include <string.h> void combine(char *src,char *out,int start,int rec,int end, int n) { int i; if(n>end) n=end; for(i=start;i<end;i++){ out[rec]=src[i]; if(n==1){ out[rec+1]='\0'; printf("%s\n",out); }else { combine(src,out,i+1,rec+1,end,n-1); } } } int main(int argc, char *argv[]) { char out[strlen(argv[1])]; combine(argv[1],out,0,0,strlen(argv[1]),atoi(argv[2])); return 0; }
#include <stdio.h> #include <string.h> void arrange(char *src,int start,int end) { int i; char tmp; if(start==end){ printf("%s\n",src); } for(i=start;i<end;i++){ tmp=src[start];src[start]=src[i];src[i]=tmp; arrange(src,start+1,end); tmp=src[start];src[start]=src[i];src[i]=tmp; } } int main(int argc, char *argv[]) { arrange(argv[1],0,strlen(argv[1])); return 0; }