C++在while循环中修改变量

3
我是C++的新手,试图通过[Project Euler][1]来学习。我一直到达了[Problem 4][2](令人印象深刻),但在while循环中遇到了变量作用域问题。如果你不知道,这个问题要求你找到两个三位数的最高回文积。我制作了一个while循环,应该测试一个积是否为回文(我将其放入另一个函数中-它工作得很好)。
这是我的当前代码(尽管它已经改变了很多次-我试图使这个代码最明确,这就是为什么有所有的else if):
int main()
{

    int int1 = 999;
    int int2 = 999;
    int nProduct = int1 * int2;
    int nFinalProduct = 0;

    while (int1 >= 100)
    {
        if (paltest(nProduct) == 1 && nProduct > nFinalProduct && int2 > 100)
        {
            nFinalProduct = nProduct;
            --int2;
        }
        else if (paltest(nProduct) == 1 && nProduct > nFinalProduct
                 && int2 == 100)
        {
            nFinalProduct = nProduct;
            --int1;
        }
        else if (paltest(nProduct) == 0 && int2 > 100)
        {
            --int2;
        }
        else if (paltest(nProduct) == 0 && int2 == 100)
        {
            --int1;
        }
    }
    cout << nFinalProduct;
}

我想说的是,如果产品是回文且高于之前的产品,则将其添加到nFinalProduct中,并将int1或int2减少以获得下一个产品。
我已经尝试多次重写main(),使用相同的逻辑,但每次输出都不会改变(在这种情况下为0)。它只更新while循环内部的值,然后在循环结束时重置它吗?我的Project Euler第三个问题的解决方案使用了相同的思想,即初始化一个变量,在while循环中更改它,并在循环外打印它,这很好用。除了可能从未发现paltest()为1之外,我无法想出问题所在,我已经进行了大量测试,没有找到问题。
感谢任何帮助。
更新:好的,谢谢大家。我将nProduct声明移动到while循环内部,现在它不会结束。这是我的新代码:
int main(){

    int int1 = 999;
    int int2 = 999;
    int nFinalProduct = 0;

    while (int1 >= 100){

        int nProduct = int1 * int2;

        if (paltest(nProduct) == 1 && nProduct > nFinalProduct && int2 > 100){
            nFinalProduct = nProduct;
            --int2;
        }
        else if (paltest(nProduct) == 1 && nProduct > nFinalProduct && int2 == 100){
            nFinalProduct = nProduct;
            int2 = 999;
            --int1;
        }
        else if (paltest(nProduct) == 0 && int2 > 100){
            --int2;
        }
        else if (paltest(nProduct) == 0 && int2 == 100){
            int2 = 999;
            --int1;
        }
    }
    cout << nFinalProduct;
}

现在这个程序将会无限运行。我的感觉是int1从未被减少(这最终会终止循环)。如果它没有被减少,那么意味着int2也从未减少。我是否正确?

[1] https://projecteuler.net [2] https://projecteuler.net/problem=4


这很可能意味着没有一个重新分配给 nFinalProduct 的代码路径被执行,这意味着检查它们的条件从未得到满足。 - greatwolf
2
你应该真正接受解决这个问题的答案并开始一个新的问题。 - kfsone
4个回答

0

(请注意 - 我知道这是几年前提出的非常老的问题。我只是为了完整性而添加此答案,因为没有其他答案解决第二个代码中的问题。不指望任何UV)

你的第二段代码无法结束的原因是:

在计算过程中,它找到了 int1 = 995int2 = 583 值的第一个回文数,即580085,并将其分配给 nFinalProduct 变量。

第二个回文数是对于int1 = 995int2 = 517 值,即 514415nProduct 的值为 514415)。

检查您的代码,针对int1 = 995int2 = 517nProduct = 514415nFinalProduct = 580085值,在while循环内部的if条件。没有一个if条件结果为true,因此int1int2值没有变化,且int1值为995(>= 100),因此while循环会一直循环下去。

代码编写得过于复杂,难以阅读。多次调用相同值的paltest()是不必要的。可以简化如下:

int main(){

    int int1 = 999;
    int int2 = 999;
    int nFinalProduct = 0;

    while (int1 >= 100){

        int nProduct = int1 * int2;

        int res = paltest(nProduct);

        if ((res == 1) && (nProduct > nFinalProduct)) {
            nFinalProduct = nProduct;
        }

        --int2;

        if ((int2 < 100) || ((res == 1) && (nProduct < nFinalProduct))) {
            int2 = 999;
            --int1;
        }
    }
    cout << nFinalProduct;
}

0

看起来你代码中的一个问题是循环中nProduct从未更新。你在循环外将其初始化为999*999,并且在每次循环迭代中都保持不变。因此,你一直在检查相同的数字。

我会让你自己想办法解决这个问题。

编辑:你的回文检查函数没有处理一般情况。将其重构以处理一般情况非常简单:

bool isPalindrome(int nProduct)
{
  string subject = to_string(nProduct);
  for(int i = 0, n = subject.length(); i < n / 2; ++i)
  {
    if(subject[i] != subject[n - i - 1]) return false;
  }

  return true;
}

0
如果你想更新 "nProduct",那么只需要更改这句话:'nProduct = int1 * int2;',然后它将在每次迭代中发生变化。

0
如果我理解问题正确的话,您想在每次循环迭代中更新“nProduct”。所以唯一的变化就是将“nProduct = int1 * int2;”放在“while (int1 >= 100){”的下面。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接