密码破解组合计划

时间:2014-11-28 22:31:28

标签: c++ for-loop stl combinations

我在执行任务时遇到了一些麻烦。我有一个30字的字符串数组。我必须将这些单词中的三个组合成一个密码组合。诸如“airairair”或“allallall”的组合是可接受的。我知道我应该有27000种组合(30 * 30 * 30),但到目前为止我只有900种。有人可以帮助我吗?我究竟做错了什么?代码如下:

#include<iostream>
#include<string>
#include<list>
#include<fstream>
using namespace std;

int main(int argc, char **argv[])
{
    string passwords[] = { "air", "few", "all", "one", "cot", "jam", "sip", "gag", "arc", "egg",
        "had", "hut", "tan", "paw", "pay", "got", "get", "pea", "rig", "cop",
        "sat", "two", "who", "six", "sow", "dam", "tip", "lit", "awl", "dew" };

    static int numWords = 0;
    static int count = 0;
    int arrLength = sizeof(passwords) / sizeof(passwords[0]);

    ofstream f("passwords.dat"); //write passwords to file
    int i, j, k;

    for (i = 0; i < arrLength; i++)
    {
        for ( j = 0; i < arrLength; i++)
        {
            for ( k = 0 ; k < arrLength; k++)
            {
                cout << passwords[i] << passwords[j] << passwords[k];
                f << passwords[i] << passwords[j] << passwords[k] << "\n";
            }
        }
    }
    f.close();
    system("pause");

    return 0;
}

1 个答案:

答案 0 :(得分:3)

您的J循环错误。它应该是for( j=0 ; j < arrLength; j++)

然而,它输错了 for( j=0 ; i < arrLength ; i++ )

因此,它只能提供30 * 30种组合,而不是30 * 30 * 30