回文和项目Euler C++版本-技巧

回文和项目Euler C++版本-技巧,c++,C++,有人能告诉我我的代码出了什么问题吗 谢谢各位: // A palindromic number reads the same both ways. // The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. // Find the largest palindrome made from the product of two 3-digit numbers. #include

有人能告诉我我的代码出了什么问题吗

谢谢各位:

// A palindromic number reads the same both ways.
// The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.

#include <iostream>
using namespace std;

int main()
{
    int x = 2;
    int  product;
    int  n, digit, rev = 0;
    int  greatest = 0;

    for(int i = 2;i<100;i++){
        product = x * i;
        n = product;
        cout << x << " * " << i << " = " << product << endl;
        do
        {
            digit = product % 10;
            rev = (rev * 10) + digit;
            product = product / 10;
        } while(product != 0);
        cout << " The reverse of the number is: " << rev << endl;
        if(n == rev){
            cout << "Therefore Palindrome" << endl;
            if(rev > greatest){
                cout << "REV Greater Than Greatest Palindrome" << endl;
                greatest = rev;
            }
        }
        if(i == 99){
            if(x < 99){
                x++;
                i = 1;
                cout << "Go For The Next Loop" << endl;
            }
        }
    }
    cout << "The Greatest Palindrome Number Is " << greatest << endl;
    return 0;
}
有几件事:

你应该从999到100倒计时。 对于产品的两个要素,应该使用两个循环 在每次进入内部do while循环之前,rev最初应为零 结果看起来是这样的,除了检测到的回文和最终结果外,所有的结果都是输出的。输出中的无用噪声量令人费解

#include <iostream>
using namespace std;

int main()
{
    int  product;
    int  n, digit, rev = 0;
    int  greatest = 0;

    for(int i = 999;i>=100; --i)
    {
        for (int j =999; j>=100; --j)
        {
            product = j * i;
            n = product;
            rev = 0;
            do
            {
                rev = (rev * 10) + (product % 10);
                product /= 10;
            } while(product != 0);

            if(n == rev)
            {
                cout << "Palindrome : " << i << " * " << j << " = " << n << endl;
                if(rev > greatest)
                    greatest = rev;
            }
        }
    }
    cout << "The Greatest Palindrome Number Is : " << greatest << endl;
    return 0;
}

你得到了什么输出?你希望得到什么样的输出?请贴一个帖子。你计算产品反面的方法是行不通的。它只需将每个循环乘以10,然后将最后一个数字相加。你有整数溢出。一个好的开始是检查三位数,而不是一位数和两位数。调试器。使用调试器。调试器允许您一次执行一条语句,并监视变量的值。比上传到StackOverflow并等待别人为你调试你的程序快多了。你的回答很好,非常感谢,但有一件事我不明白,为什么我应该使用两个循环,它让我感到困扰。你能告诉我变量值将如何变化,以及程序将如何处理变量值。。。这会导致我的产品相同吗?还有一件事。。。。我被告知除非必要,否则不要使用break,这会让你成为一个糟糕的程序员,非常感谢先生,非常感谢你不必使用两个循环。但实际上,这就是你在代码中用x的增量和i的重置所做的。关于使用break,在某些情况下,它是在循环条件中编码复杂度的一种优雅的替代方法,事实上,它为代码带来了清晰度,而不是混淆。这是一个合理的例子。最后,造成糟糕程序员的最相关因素是糟糕的指令,无论是人工指令、文本指令还是简单的指令都不够,但这真的更好吗?不完全是。你是一个真正的领袖和哲学家,先生。。。。你能给我更多启发吗?你为什么用“我”而不是“谢谢”呢?我真不敢相信你做了这一切,先生
#include <iostream>
using namespace std;

int main()
{
    int  greatest = 0;

    for(int i = 999;i>=100; --i)
    {
        for (int j =990; j>=100; j-=11)
        {
            int product = j * i;
            if (product < greatest)
                break;

            int n = product;
            int rev = 0;
            do
            {
                rev = (rev * 10) + (product % 10);
                product /= 10;
            } while(product != 0);

            if(n == rev)
            {
                cout << "Palindrome : " << i << " * " << j << " = " << n << endl;
                if(rev > greatest)
                    greatest = rev;
            }
        }
    }
    cout << "The Greatest Palindrome Number Is : " << greatest << endl;
    return 0;
}
Palindrome : 995 * 583 = 580085
Palindrome : 993 * 913 = 906609
The Greatest Palindrome Number Is : 906609