for循环内的if语句未执行

时间:2020-08-15 03:04:16

标签: c++ function for-loop if-statement

编写一个程序来解决项目欧拉的问题四:查找由两个2位数字的乘积构成的最大回文。这是我的代表:

#include <iostream>

int reverseNumber(int testNum)
{

    int reversedNum, remainder = 0;
    int temp = testNum;

    while(temp != 0)
    {
        remainder = temp % 10;
        reversedNum = reversedNum * 10 + remainder;
        temp /= 10;
    }

    return reversedNum;
}

int main()
{
    const int MIN = 100;
    int numOne = 99;
    int product = 0;

    for(int numTwo = 10; numTwo < 100; numTwo++)
    {
        product = numOne * numTwo;

        if (reverseNumber(product) == product)
        {
            int solution = product;
            std::cout << solution << '\n';
            return 0;
        }        
    }

    return 0;
}

我背后的主要思想过程是for循环将遍历从10到99的每个数字并将其乘以99。我的预期结果是让它打印9009,这是具有2位2位数的2个因子的最大回文。因此,我认为应该在这里进行的for循环将从10变为99,并且每个循环都应通过if语句的参数,该语句反转数字并查看其是否相等。 我已经确定这不是编译器问题,因为这在不同的编译器之间反复发生。每当我对其进行测试时,reverseNumber()函数都会返回正确的数字,因此这不应该是问题,但是仅当该函数参与逻辑比较时才会出现此问题。我的意思是,即使我将其设置为变量并将其放入if参数中,问题仍然存在。我很沮丧。我只是希望这不是一些愚蠢的错误,因为我已经在此问题上待了几天。

2 个答案:

答案 0 :(得分:2)

int reversedNum, remainder = 0;

您应该知道,这会给您(在自动变量上下文中)零remainder,但是任意的 reversedNum。这实际上是某些开发商店具有“每个声明一个变量”规则的原因之一。

换句话说,它应该是:

int reversedNum = 0, remainder;

甚至:

int reversedNum = 0;
int remainder;

通常有帮助的另一件事是将变量的范围限制在尽可能小的范围内,仅在需要时才将它们存在。一个例子是:

int reverseNumber(int testNum) {
    int reversedNum = 0;

    while (testNum != 0) {
        int remainder = testNum % 10;
        reversedNum = reversedNum * 10 + remainder;
        testNum /= 10;
    }

    return reversedNum;
}

实际上,由于您只使用过一次,因此我可能会更进一步淘汰remainder

reversedNum = reversedNum * 10 + testNum % 10;

您会发现我在那里也摆脱了temp。将testNum放到临时变量中并没有什么好处,因为它已经已经是原始副本的副本(因为它是通过值传递的)。


还有一个注意事项,更多的是问题而不是代码。您似乎以为回文形式为99的倍数。可能是 ,但是谨慎的程序员不会依赖它-如果允许您承担类似的事情,则可以将整个程序替换为:

print 9009

因此,您可能应该检查所有可能性。

您还将获得发现的第一个,不一定是最高的(例如,假设99 * 1799 * 29都是回文型-您不希望第一个。

/ p>

而且,由于您正在检查所有可能性,因此即使嵌套循环是递减而不是递增,您可能也不想在第一个停顿。这是因为,如果99 * 397 * 97都是回文的,则您要的是最高的,而不是第一个。

因此,更好的方法可能是从头开始并进行详尽的搜索,同时还要确保忽略对小于当前最大值的候选者的回文检查,例如(伪代码)

# Current highest palindrome.

high = -1

# Check in reverse order, to quickly get a relatively high one.

for num1 in 99 .. 0 inclusive:
    # Only need to check num2 values <= num1: if there was a
    # better palindrome at (num2 * num1), we would have
    # already found in with (num1 * num2).

    for num2 in num1 .. 0 inclusive:
        mult = num1 * num2

        # Don't waste time doing palindrome check if it's
        # not greater than current maximum - we can't use
        # it then anyway. Also, if we find one, it's the
        # highest possible for THIS num1 value (since num2
        # is decreasing), so we can exit the num2 loop
        # right away.

        if mult > high:
            if mult == reversed(mult):
                high = mult
                break

if high >= 0:
    print "Solution is ", high
else:
    print "No solution"

答案 1 :(得分:1)

除了正确初始化变量外,如果您想要最大的回文,还应该切换for循环的方向-例如:

for(int numTwo = 100; numTwo > 10; numTwo--) { 
    ...
}

否则,您只是在指定范围内打印第一个回文图