1. 程式人生 > 其它 >遍歷密碼演算法

遍歷密碼演算法

遍歷密碼演算法

#include "iostream"
#include "vector"
#include "string"

using namespace std;

int findPwd(int deep, string parent);

/*密碼組成,有數字,小寫字母,大寫字母組成*/
static char charSource[] = { '0', '1', '2','3','4','5','6','7','8','9',
                            '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','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'}; /*密碼*/ string password = "000A0"; int main() { /*
密碼長度*/ static int pswLength = 5; string pwd = ""; for (int j=0; j < sizeof(charSource); j++) { if (findPwd(pswLength, pwd + charSource[j])) { break; } } getchar(); return 0; } /*遞迴尋找密碼*/ int findPwd(int deep, string parent) { /*遞迴直到最後一層
*/ if (deep == 1) { /*判斷是否與密碼相同,相同返回1,否則返回0*/ if (parent == password) { cout << "I had find the password: " << parent; return 1; } else return 0; } else { for (int j = 0; j < sizeof(charSource); j++) { /*遞歸併判斷子樹返回值,如若返回1,則函式結束,如返回0,繼續往下尋找*/ if (findPwd(deep - 1, parent + charSource[j])) { return 1; } } } return 0; }