暴力破解密码 - C++ 递归方法实现
最编程
2024-08-12 08:35:59
...
问题描述:
暴力破解密码
假设有一个 4 位字母密码,每位密码是 a~e 之间的小写字母
你能否编写一段代码,来暴力破解该密码?(可重复排序)
#include <iostream>
#include<string>
using std::string;
using namespace std;
void BreakPassword( string Words, int PasswordLen, string result)
{
if (result.length() == PasswordLen)
{
//C++中string类型不能直接输出,需加头问题#include<string>,不能用#include<string.h>
cout << result << " ";
return;
}
for (int i = 0; i < Words.length(); ++i)
{
string newResult = result;
newResult.insert( newResult.end(), Words[i] );
BreakPassword(Words, PasswordLen, newResult);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int passwordLen = 4;
string words("abcde");
string result = "";
BreakPassword(words, passwordLen, result);
return 0;
}
下一篇: C语言中实现全排列的递归算法