Mit*_*ell 0 c combinations permutation
有人可以帮我得到一个C算法来生成长度为n的所有字母组合吗?
我需要的输出是这样的:
aaaaaaa
aaaaaab
aaaaaac
.
.
.
zzzzzzx
zzzzzzy
zzzzzzz
for(i = 0; i<length; i++){
pass[i] = 'a';
}
while(1){
for(j=0;j<26;j++){
printf("%s\n",pass);
pass[i] = (char)(pass[i]+1);
}
if(pass[i-1]==z)...
}
return 0;
Run Code Online (Sandbox Code Playgroud)
这是使用递归的版本:
#include <stdio.h>
#include <string.h>
void iterate(char *str, int idx, int len) {
char c;
if (idx < (len - 1)) {
for (c = 'a'; c <= 'z'; ++c) {
str[idx] = c;
iterate(str, idx + 1, len);
}
} else {
for (c = 'a'; c <= 'z'; ++c) {
str[idx] = c;
printf("%s\n", str);
}
}
}
#define LEN 3
int main(int argc, char **argv) {
char str[LEN + 1];
memset(str, 0, LEN + 1);
iterate(str, 0, LEN);
}
Run Code Online (Sandbox Code Playgroud)