遍历密码算法

遍历密码算法

#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;
}

 

posted @ 2022-03-04 13:04  电子_精灵  阅读(90)  评论(0编辑  收藏  举报