c++ - 密码破解码合程序

标签 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;
}

最佳答案

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

然而,它被错误地输入为 for( j=0 ; i < arrLength ; i++ )

因此它只会给出 30*30 个组合而不是 30*30*30

关于c++ - 密码破解码合程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196711/

相关文章:

c++ - 嵌套循环中的 OpenMP 数组索引

c++ - 并排使用 for 循环和 if 语句 C++

windows - 丢失批处理文件 FOR 语句的 (set) 元素中命令的错误输出

c++ - 检查迭代器

c++ - 使用分配器概念时如何避免堆栈上的临时对象?

c++ - 错误 : multiple definiton of class function c++

C++将两个不同类型的 vector 的属性连接到新 vector 中

c++ - SFINAE 方法比较

c++ - 为什么对字符串参数的 const 引用可以采用字符串文字?

c++ - 排序并跟踪元素