字符串:基于给定长度的所有组合

编程入门 行业动态 更新时间:2024-10-09 00:44:34
本文介绍了字符串:基于给定长度的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

HI, 我想基于多个字符生成字符串的所有组合.例如,如果我输入数字"5",则我的程序应生成5个字母的所有可能单词.

I want to generate all combinations of a string based on the a number of characters. For instance, if I enter the number "5" then my program should generate all the possible words with 5 letters. Please anyone can help me to start this logic?

推荐答案

我有您的代码: I have YOUR code: #pragma once #include <stdio.h> #include <tchar.h> #include <malloc.h> #define AC(A) (sizeof(A)/sizeof(A[0])) const TCHAR __all_chars[] = __T("abcdefg"); int increment(unsigned int* used,const unsigned int count) { unsigned int ix; for(ix=0;(ix<count) && ((AC(__all_chars)-1)<=(++used[ix]));used[ix]=0,ix++); return ix<count; } void build(TCHAR* buff,unsigned int* used,const unsigned int count) { unsigned int ix; for(ix=0;ix<count;ix++) buff[ix] = __all_chars[used[ix]]; buff[ix] = 0; } void combine(TCHAR* buff,const unsigned int count) { unsigned int* used = (unsigned int*)malloc(sizeof(unsigned int)*count); unsigned int ix,done=0; for(ix=0;ix<count;ix++) used[ix] = 0; do { build(buff,used,count); ++done; _tprintf(__T("%04i: %s\r\n"),done,buff); } while(increment(used,count)); free(used); } int _tmain(int argc, _TCHAR* argv[]) { TCHAR s[256]; TCHAR* e; unsigned int n; if(_getts_s(s,AC(s))) { n = _tcstoul(s,&e,10); if(0<n) { TCHAR* buff = (TCHAR*)malloc(sizeof(TCHAR)*(1+n)); combine(buff,n); free(buff); } } _gettch(); return 0; }

问候.

请参阅此链接 单击 see this link Click

更多推荐

字符串:基于给定长度的所有组合

本文发布于:2023-11-30 07:30:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649174.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字符串   长度

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!