递归算法---字符串---全/部分组合和全排列
#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; }
相关推荐
Joymine 2020-03-04
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
徐建岗网络管理 2020-07-28
lynjay 2020-06-14
AaronPlay 2020-06-13
88384957 2020-06-12
herohope 2020-06-10
adwen00 2020-06-09
KilluaZoldyck 2020-06-06
hitxueliang 2020-06-05
lightindark 2020-06-03
baishuwei 2020-06-03
jediaellu 2020-05-31
twater000 2020-05-29
ItBJLan 2020-05-11
韩学敏 2020-05-09