我最近開始了一個用 C 語言處理暴力密碼破解和加密的個人專案。我一直在嘗試開發一個輸出長度為 N 的字母表的所有可能組合的函式。例如,如果 N = 4,則必須輸出 aaaa - zzzz 的所有可能性。從邏輯上講,我不明白我應該如何遞回地處理這個問題。
char* alphabet[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
void passwords(int size){
char* password[size];
char* result;
//Determining static letter
for(int i = 0; i < size; i ){
for(int x = 0; x < size; x ){
password[x] = "a";
}
int index = i 1; //password index to modify
while(index < size){
for(int j = 0; j < 26; j ){
password[i] = alphabet[j];
printf("%s\n",password);
}
index ;
}
}
}
int main(int argc, char* argv[]){
passwords(3);
return 0;
}
目前,該程式僅修改字母表中的一個字符并產生以下輸出:
aaa
baa
caa
daa
...//e-z aa
aaa
aba
aca
ada
...//a e-z a
aaa
aab
aac
aad
...//aa e_z
任何建議都會非常感謝!
uj5u.com熱心網友回復:
如果遞回的使用不是強制性要求,請嘗試:
#include <stdio.h>
#include <stdlib.h>
#define N 26 // number of alphabets
void passwords(int size) {
int i;
char *password;
if (NULL == (password = malloc(size 1))) {
perror("malloc");
exit(1);
}
for (i = 0; i < size; i ) {
password[i] = 'a'; // initialize the array
}
password[i] = '\0'; // terminate the string
while (1) {
printf("%s\n", password);
password[size - 1] ; // increment rightmost character
for (i = size - 1; i >= 0; i--) {
if (password[i] >= 'a' N) { // carry over
if (i == 0) { // end of permutation
free(password);
return;
} else {
password[i] = 'a';
password[i - 1] ;
}
}
}
}
}
int main()
{
passwords(3);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439500.html
上一篇:為什么NAS網路共享上的PowerShell腳本失敗
下一篇:問題直觀理解爬樓梯問題的解法,為什么不是t(n-1) t(n-2)不是t(n)=t(n-1) t(n-2) 2?