只有最后一个字符递增。我理解我的问题,我可以用b $ b来修复数组中每个空格的for循环,但是我想知道 如果有更好的话解决方案。 解决方案 Ken Human< ke ****** @ comcast.net>写道: 我想以编程方式生成字符0-9,A-Z和a-z的每个可能的16个字符组合。我现在的代码如下: 真的,每一个? 你希望在哪台电脑上运行这个? b $ b - Chris。 Ken Human写道:我想要生成每个可能的16个字符组合的字符0-9,AZ和az以编程方式。 你确定吗?那是'pow(62,16),这是很多组合。 关于4.8e28,我想。 我现在的代码如下: #include< stdio.h> #include< ctype.h> int main(){ char strBuf [17] = {0} ;对于(i = 0; i< 16; i ++){ for(j =''0''; j< = ''z''; j ++){如果(!isalnum(j))继续; strBuf [i] = j; printf("%s \ n",strBuf) ); } 返回0; } 只有最后一个字符递增。我理解我的问题,我可以用数组中每个空格的for循环修复它,但我想知道是否有更好的解决方案。 未经测试的代码块: { char foo [] =" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" " abcdefghijklmnopqrstuvwxyz" ;; unsigned nfoos = sizeof foo / sizeof * foo; unsigned ndx [16] = {0},i; while (ndx [15]!= nfoos) { for(i = 0; i< 16; i ++)putchar(foo [ndx [i]]); putchar(''\ n''); for(i = 0; i< 16; i ++) { ndx [i] ++; 如果(ndx [i]< nfoos)中断; ndx [i] = 0; } } } Chris McDonald写道: Ken Human< ke ****** @ comcast.net>写道: 我想以编程方式生成字符0-9,A-Z和a-z的每个可能的16个字符组合。我现在的代码如下: 真的,每一个?你希望在哪台电脑上运行这个? 我从来都不擅长数学,是可能组合的数量 62 ^ 16?谢谢你的关心。我在Plasmo Mag-8上运行它, 可以在1秒内计算出这个确切的数据量。不用说说,它也有无限的内存。 让我们说我想要一个4个字符的组合。 I want to generate every possible 16 character combination of thecharacters 0-9, A-Z, and a-z programatically. My current code follows:#include <stdio.h>#include <ctype.h>int main() {char strBuf[17] = {0};int i, j;for(i = 0; i < 16; i++) {for(j = ''0''; j <= ''z''; j++) {if(!isalnum(j)) continue;strBuf[i] = j;printf("%s\n", strBuf);}}return 0;}Only the last character increments. I understand my problem and I canfix it with a for loop for each space in the array, but I''d like to knowif there''s a better solution. 解决方案 Ken Human <ke******@comcast.net> writes: I want to generate every possible 16 character combination of thecharacters 0-9, A-Z, and a-z programatically. My current code follows:Really, every one?And on what computer are you hoping to run this?--Chris.Ken Human wrote: I want to generate every possible 16 character combination of the characters 0-9, A-Z, and a-z programatically.Are you sure? That''s pow(62,16), which is a lot of combinations.About 4.8e28, I think. My current code follows: #include <stdio.h> #include <ctype.h> int main() { char strBuf[17] = {0}; int i, j; for(i = 0; i < 16; i++) { for(j = ''0''; j <= ''z''; j++) { if(!isalnum(j)) continue; strBuf[i] = j; printf("%s\n", strBuf); } } return 0; } Only the last character increments. I understand my problem and I can fix it with a for loop for each space in the array, but I''d like to know if there''s a better solution.untested code block:{char foo[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ""abcdefghijklmnopqrstuvwxyz";unsigned nfoos = sizeof foo/sizeof *foo;unsigned ndx[16] = {0}, i;while(ndx[15] != nfoos){for (i = 0; i < 16; i++) putchar(foo[ndx[i]]);putchar(''\n'');for(i = 0; i < 16; i++){ndx[i]++;if (ndx[i] < nfoos) break;ndx[i] = 0;}}}Chris McDonald wrote: Ken Human <ke******@comcast.net> writes:I want to generate every possible 16 character combination of thecharacters 0-9, A-Z, and a-z programatically. My current code follows: Really, every one? And on what computer are you hoping to run this?I was never very good at math, is the number of possible combinations62^16? Thank you for your concern. I''m running it on the Plasmo Mag-8,which can calculate this exact amount of data in 1 second. Needless tosay, it also has an unlimited amount of memory.Let''s say that I want a 4 character combination. 这篇关于字符串生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 09:25