365bet亚洲版登录-bet官网365入口

365bet亚洲版登录拥有超过百间客房,bet官网365入口的文化历经几十年的传承和积淀形成的核心内容获得业界广泛的认可,365bet亚洲版登录是目前信誉最高的娱乐场所,同国内外几百家网上内容供应商建立了合作关系。

字符组合排序

1.将给定字符组合后,按字典序排序输出,示例:输入:a, b, c;输出:abc,acb,bac,bca,cab,cba2.将给定字符组合后,按字典序排序输出,示例:输入:a, b, c;输出:a,b,c,ab,ac,bc, abc

//Example 1#include <stdio.h>#include <string.h>void combchar(char* str, int from, int to){ if(from >= to){ for (int i = 0; i <= to; ++i){ PRINT("%c", str[i]); } PRINT; return; } int j = 0; for(int i = from; i <= to; i++){ char tmp = str[i]; for(j = i; j-1 >= from; --j){ str[j] = str[j-1]; } str[j] = tmp; combchar(str, from+1, to); for(j = from; j+1 <= i; ++j){ str[j] = str[j+1]; } str[j] = tmp; if(from == 0) PRINT; }}int main(){ char str[] = {'a', 'b', 'c', 'd'}; combchar(str, 0, sizeof; return 0;}

Example 1 Result 结果如下所示:

Example 1 Result:abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdca cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba

//Example 2#include <stdio.h>#include <string.h>#define PRINT printfint num = 0;void combination(char* str, int from, int to, int len, int t){ if{ PRINT("%cn", str[from]); ++num; for(int j = from+1; j <= to; j++){ PRINT("%*cn", 3*t+1, str[j]); ++num; } return; } for(int j = from; j <= to-len+1; j++){//1,2,3,4 PRINT("%*c->",  ? 1 : 3*t+1, str[j]); combination(str, j+1, to, len-1, t+1); }}void combination2(char* str, int from, int to, int len, int t, char* tmpStr){ if{ for(int j = from; j <= to; j++){ tmpStr[t] = str[j]; for(int i = 0; i <= t; i++){ PRINT("%c", tmpStr[i]); } PRINT; } }else{ for(int j = from; j <= to-len+1; j++){//1,2,3,4 tmpStr[t] = str[j]; combination2(str, j+1, to, len-1, t+1, tmpStr); } } if PRINT;}void findSubStr(char* str, int from, int to){ char tmpStr[from-to+1]; for(int j = 1; j <= to-from+1; j++){//len=1,2,3,4 //combination(str, from, to, j, 0); combination2(str, j+1, to, len-1, t+1, tmpStr); PRINT("-------------------%d|%dn", j, num); }}int main(){ char str[] = {'a', 'b', 'c', 'd'}; int size = strlen; findSubStr(str, 0, size-1); return 0;}

Example 2 Result 结果如下所示:

Example 2 combination Result:abcda->b c db->c dc->da->b->c d c->db->c->da->b->c->dExample 2 combination2 Resulta b c d ab ac ad bc bd cd abc abd acd bcd abcd 

本文由365bet亚洲版登录发布于计算机网络,转载请注明出处:字符组合排序

您可能还会对下面的文章感兴趣: