密码谜题

Cryptarithmetic puzzle

我正在尝试解决这个密码难题 TWO + TWO = 4,我使用了原始蛮力,但我无法弄清楚我在哪里犯了错误。这里的想法是它尝试从 0 到 10 的所有可能的数字组合,并且分配给字符的所有数字必须不同。根据定义

a cryptarithmetic puzzle is a mathematical game where the digits of some numbers are represented by letters (or symbols). Each letter represents a unique digit. The goal is to find the digits such that a given mathematical equation is verified: In this case:

   TWO
 + TWO
 ------ 
= FOUR

此代码遍历所有可能的组合,直到找到满足问题的解决方案。 else if 语句中给出了它的约束。 第一个 if 语句只是检查数字是否相同,如果相同,则跳过该迭代。

我想要的输出是看到显示所有 个正确的解决方案。

int T, W, O, F, U, R;

for (T = 0; T < 10; T++)
{
    for (W = 0; W < 10; W++)
    {
        for (O = 0; O < 10; O++)
        {
            for (F = 0; F < 10; F++)
            {
                for (U = 0; U < 10; U++)
                {
                    for (R = 0; R < 10; R++)
                    {
                        if ((T == W) || (T == O) || (T == F) || (T == U) || (T == R) || (W == O) || (W == F) || (W == U) || (W == R) || (O == F) || (O == U) || (O == R) || (F == U) || (F == R) || (U == R))
                        {
                            continue;
                        }
                        else if (200 * T + 20 * W + 2 * O == F * 1000 + O * 100 + U * 10 + R * 0) {
                            cout << "T = " << T << endl
                                << "W = " << W << endl
                                << "O = " << O << endl
                                << "F = " << F << endl
                                << "U = " << U << endl
                                << "R = " << R << endl << endl;
                            break;
                        }
                    }
                }
            }
        }
    }
}

我得到了一堆结果,有趣的是,只有最后一个结果很好,它给出了:

T = 7
W = 6
O = 5
F = 1
U = 3
R = 0

R * 0 应该是 R * 1

你得到的答案恰好是 R = 0 的答案(因为当 R0 时,R * 0R * 1).我猜你也想从 1 开始 F 因为通常密码不允许前导零。