对于一个给定的字符集合,将他们的所有组合进行打印。
- #ifndef _LOCAL_ZUHE_H_
- #define _LOCAL_ZUHE_H
- #define MAX 80
- #define TRUE 1
- #define FALSE 0
- void zuhe(char *,int,char *);
- int puanduan(char *,int);
- void show(char *);
- #endif
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include"local_zuhe.h"
- int length;
- void show(char *pans)
- {
- int in = 0;
- printf("************************************************************************************\n");
- for( ; in < length;in++)
- printf("%c ",pans[in]);
- printf("&***********************************************************************************\n");
- }
- int panduan(char *ans, int len)
- {
- int index =0;
- int count[128] = {0};
-
- if(len == 0)
- return FALSE;
- for(index =0 ; index<len; index++) {
- count[ans[index]-'0']++;
- if(count[ans[index]-'0'] >1)
- return FALSE;
- }
- return TRUE;
- }
- void Hzuhe(char *ans,int len)
- {
- char * res = malloc( len *sizeof *res);
- if(res == NULL)
- goto out;
- else
- zuhe(res,len,ans);
- out:
- free(res);
- res = NULL;
- }
- void zuhe(char * ans, int len, char *p)
- {
- int ij;
- if(len >0) {
- for(ij =0 ; ij < length;ij++) {
- ans[length-len] =p[ij];
- if(TRUE == panduan(ans,strlen(ans)))
- zuhe(ans,len-1,p);
- ans[length-len]=0;
- }
- } else
- show(ans);
- }
- int main()
- {
- char *p,*pans;
- p = malloc(MAX * sizeof *p);
- printf("please input the comnibation of the String\n");
- pans = fgets(p,MAX,stdin);
- if(NULL==pans)
- goto out;//来自内核代码
- length = strlen(pans)-1;
- Hzuhe(pans,length);
- out:
- free(pans);
- pans = p = NULL;
- return 0;
- }
测试之后发现,该程序只适用于无相同字符的字符串中。因此进行一下修改。以下标作为重新组合的参考因素对于相同字符则对其中一个进行全面的组合
例如:
a b a
发现有2个a,
a b a
a a b
b a a
b a a
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include"local_zuhe.h"
- int length;
- void show(char *pans,char *pox)
- {
- int in = 0;
- for( ; in < length;in++)
- printf("%c ",pox[pans[in]-'0']);
- printf("\n&***********************************************************************************\n");
- }
- int panduan(char *ans, int len)
- {
- int index =0;
- int count[128] = {0};
-
- if(len == 0)
- return FALSE;
- for(index =0 ; index<len; index++) {
- count[ans[index]-'0']++;
- if(count[ans[index]-'0'] >1)
- return FALSE;
- }
- return TRUE;
- }
- void Hzuhe(char *ans,int len)//对传来的字符串中的内容进行统计。设置不同的标志,如果其中有相同的字符,则跳过
- {
- char * res = malloc( len *sizeof *res);
- char * flag = malloc( (len+1) *sizeof *flag);
- if(res == NULL || flag == NULL)
- goto out;
- else{
- int index=0;
- int ele[256] = {0};
- for(index=0; index < len;index++)
- { if(ele[ans[index]]>0)
- flag[index]=(len+'0');
- else
- flag[index]='0';
- ele[ans[index]]++;
- }
- flag[index]=0;
- zuhe(res,len,flag,ans);
- }
- out:
- free(res);
- free(flag);
- res = NULL;
- }
- void zuhe(char * ans, int len, char *p,char *pox)
- {
- int ij;
- if(len >0) {
- for(ij =0 ; ij < length;ij++)
- if(p[ij]=='0'|| length){
- ans[length-len] =ij+'0';
- if(TRUE == panduan(ans,strlen(ans)))
- zuhe(ans,len-1,p,pox);
- ans[length-len]=0;
- }
- } else {
- show(ans,pox);
- }
- }
- int main()//该程序没有考虑到在待排序的字符中有相同的字符。
- {
- char *p,*pans;
- p = malloc(MAX * sizeof *p);
- printf("please input the comnibation of the String\n");
- pans = fgets(p,MAX,stdin);
- if(NULL==pans)
- goto out;
- length = strlen(pans)-1;
- Hzuhe(pans,length);//将获取的字符串传入Hzuhe
- out:
- free(pans);
- pans = p = NULL;
- return 0;
- }
阅读(831) | 评论(0) | 转发(0) |