【问题标题】:I am not getting desired output in reverse a number code in C++我在 C++ 中没有得到所需的反向数字代码输出
【发布时间】:2020-07-20 10:31:08
【问题描述】:

我试图找到一个反转的数字,并通过不同的方法检查它是否是回文,但我得到了一个正确的反转数字,最多两位数,如果数字超过两位,那么我得到错误的输出.我不明白为什么会这样,因为我认为我的代码是正确的。

下面是代码

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int num, rem, t, add;
    cin >> t;

    while (t--) {
        int total = 0, count = 0, i = 1, quo = 0;
        cin >> num;
        quo = num;

        while (quo > 9) //count determiner
        {
            quo = quo / 10;
            ++count;
        }

        while (count >= 0) //reverse number saved in total
        {
            int den = pow(10, i);
            rem = (num % den);
            add = rem / pow(10, i - 1);

            total = total + (add * pow(10, count));

            ++i;
            --count;
        }

        if (total == num) {
            cout << "Palindrome"
                 << "\n";
        }
        else {
            cout << "Not a Palindrome"
                 << "\n";
        }
    }
    return 0;
}

请帮助我知道我在这段代码中哪里出错了。

【问题讨论】:

  • C 和 C++ 是两种截然不同的语言,具有不同的规则和语义。请仅标记您的代码实际编写的语言。另外请花一些时间阅读the help pages,阅读SO tour,阅读How to Ask,以及this question checklist
  • 您不需要pow 来反转数字。事实上,它非常简单。
  • 另外,这个数字可以是负数吗?如果是这样,您的例行程序将无法正常工作。
  • 您在使用调试器时学到了什么?
  • How to debug small programs 是每个程序员都应该能够使用的一项重要技能。使用 StackOverflow 作为调试器比使用调试器进行调试要慢几个数量级。您已经能够很快地重新考虑“我认为我的代码是正确的”。

标签: c++ reverse palindrome


【解决方案1】:

我不明白你的代码。所以我自己假设并编写代码。我假设不会有负数,如果有,那么我去掉负号。请提供负数的期望输出。

#include <iostream>
using namespace std;
int main()
{
    //int num, rem, t, add;
    int t;
    cin >> t;

    while (t-- > 0) {
        int n;
        cin >> n;
        int num = abs(n);
        if (n < 0)
        {
            n = abs(n);
        }
        int res{ 0 };
        while (n > 0)
        {
            res *= 10;
            int rem = n % 10;
            res += rem;
            n /= 10;
        }

        if (res  == num) {
            cout << "Palindrome"
                 << "\n";
        }
        else {
            cout << "Not a Palindrome"
                 << "\n";
        }
    }
    return 0;
}

以上代码的输出:

4
-191
Palindrome
232
Palindrome
123
Not a Palindrome
561
Not a Palindrome

【讨论】:

    【解决方案2】:

    您用于反转数字的代码非常复杂,因为它使用pow(一个浮点函数)来获取每个数字。如果您寻找如何反转整数的模式,这是完全没有必要的。

    只需简单的加法、乘以 10 和模数即可。请注意,我创建了一个函数,因此很容易理解:

    #include <cmath>
    #include <iostream>
    
    int reverse_int(int num)
    {
       int total = 0;
       // take care of negative by using absolute value
       int tempNum = abs(num);
       while (tempNum > 0)
       {
           total = (total*10) + (tempNum % 10);
           tempNum /= 10;
       }
       return (num < 0)?-total:total;
    }
    
    int main()
    {    
       int num = 1234321;
       if ( num == reverse_int(num))
          std::cout << num << " is a palindrome\n";
       else      
          std::cout << num << " is not a palindrome\n";
          
       int num2 = 123;
       if ( num2 == reverse_int(num2))
          std::cout << num2 << " is a palindrome\n";
       else      
          std::cout << num2 << " is not a palindrome\n";
    }
    

    输出:

    1234321 is a palindrome
    123 is not a palindrome
    

    如果您按照正在发生的事情进行循环,则非常简单:

    number = 123 (Assume this is our number)
    total = 0;
    
    Loop while (number > 0):
    
    First iteration:
    total = (total * 10) + (number % 10) --> (0 * 10) + (0 % 3) --> 3
    number /= 10 --> 12
    
    Second iteration:
    total = (total * 10) + (number % 10) = (3 * 10) + (12 % 10) --> 32
    number /= 10 --> 1
    
    Third iteration:
    total = (total * 10) + (number % 10) = (32 * 10) + (1 % 10) --> 321
    number /= 10 --> 0 (Stop the loop)
    
    total = 321
    

    在函数结束时,我们只是返回值,如果原始数字为负数,则将其设为负数。

    【讨论】:

    • 如果您要创建while (tempNum != 0),您实际上会考虑负数,并且可以摆脱 reverse_int 和 abs 调用函数末尾的负数检查。 coliru.stacked-crooked.com/a/e6d1f40ddffab7f7
    • 是的,你说得对,不需要使用 pow 函数,你写的代码我已经用这种方法解决了这个问题,我只是想从另一种方法,我无法理解为什么代码在数学上无法正常工作,我发现代码正确,但该代码给出了最多 2 位数字的正确反转数字,而对于超过 2 位数字,它给出的错误输出意味着错误的反向数字。这就是我的问题是“为什么我的代码无法正常工作,即使从我这边看所有的东西都是正确的”。
    【解决方案3】:

    您没有检查输入是否有效。所以如果我们把它放在一边并假设输入是一个有效的整数,那么你可以使用std::string 并通过std::reverse 反转它:

    #include <string>
    #include <algorithm>
    #include <iostream>
    
    int main() {
        std::string input;
        std::cin >> input;
        std::string reverse = input;
        std::reverse(reverse.begin(),reverse.end());
        if (input == reverse) std::cout << "Palindrome number"
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-24
      • 2021-06-21
      • 2021-08-28
      • 2021-03-05
      • 1970-01-01
      • 2015-05-16
      • 2016-12-12
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多