📜  整数的倒数位,已处理溢出

📅  最后修改于: 2021-04-24 04:52:26             🧑  作者: Mango

假设输入是32位整数,则编写一个程序以反转整数。如果反向整数溢出,则输出-1作为输出。
让我们看看一种简单的方法来反转整数。

C++
// A simple C program to reverse digits of
// an integer.
#include 
 
int reversDigits(int num)
{
    int rev_num = 0;
    while (num > 0)
    {
        rev_num = rev_num*10 + num%10;
        num = num/10;
    }
    return rev_num;
}
  
/* Driver program to test reversDigits */
int main()
{
    int num = 5896;
    printf("Reverse of no. is %d", reversDigits(num));
    return 0;
}


Java
// Java program to reverse a number
 
class GFG
{
    /* Iterative function to reverse
    digits of num*/
    static int reversDigits(int num)
    {
        int rev_num = 0;
        while(num > 0)
        {
            rev_num = rev_num * 10 + num % 10;
            num = num / 10;
        }
        return rev_num;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int num = 4562;
        System.out.println("Reverse of no. is "
                           + reversDigits(num));
    }
}
 
// This code is contributed by Anant Agarwal.


Python
# Python program to reverse a number
 
n = 4562;
rev = 0
 
while(n > 0):
    a = n % 10
    rev = rev * 10 + a
    n = n / 10
     
print(rev)
 
# This code is contributed by Shariq Raza


C#
// C# program to reverse a number
using System;
 
class GFG
{
    // Iterative function to
    // reverse digits of num
    static int reversDigits(int num)
    {
        int rev_num = 0;
        while(num > 0)
        {
            rev_num = rev_num * 10 + num % 10;
            num = num / 10;
        }
        return rev_num;
    }
     
    // Driver code
    public static void Main()
    {
        int num = 4562;
        Console.Write("Reverse of no. is "
                        + reversDigits(num));
    }
}
 
// This code is contributed by Sam007


PHP
 1)
    {
        $rev_num = $rev_num * 10 +
                        $num % 10;
        $num = (int)$num / 10;
    }
    return $rev_num;
}
 
// Driver Code
$num = 4562;
echo "Reverse of no. is ",
       reversDigits($num);
 
// This code is contributed by aj_36
?>


Javascript


C++
// C++ program to reverse digits
// of a number
#include 
 
using namespace std;
 
/* Iterative function to reverse
digits of num*/
int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num % 10;
 
        rev_num = (rev_num * 10) + curr_digit;
 
        // checking if the reverse overflowed or not.
        // The values of (rev_num - curr_digit)/10 and
        // prev_rev_num must be same if there was no
        // problem.
        if ((rev_num - curr_digit) /
               10 != prev_rev_num)
        {
            cout << "WARNING OVERFLOWED!!!"
                 << endl;
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num / 10;
    }
 
    return (negativeFlag == true) ?
                         -rev_num : rev_num;
}
 
// Driver Code
int main()
{
    int num = 12345;
    cout << "Reverse of no. is "
         << reversDigits(num) << endl;
 
    num = 1000000045;
    cout << "Reverse of no. is "
         << reversDigits(num) << endl;
 
    return 0;
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


C
// C program to reverse digits of a number
#include 
 
/* Iterative function to reverse digits of num*/
int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num%10;
 
        rev_num = (rev_num*10) + curr_digit;
 
        // checking if the reverse overflowed or not.
        // The values of (rev_num - curr_digit)/10 and
        // prev_rev_num must be same if there was no
        // problem.
        if ((rev_num - curr_digit)/10 != prev_rev_num)
        {
            printf("WARNING OVERFLOWED!!!\n");
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num/10;
    }
 
    return (negativeFlag == true)? -rev_num : rev_num;
}
 
/* Driver program to test reverse Digits */
int main()
{
    int num = 12345;
    printf("Reverse of no. is %d\n", reversDigits(num));
 
    num = 1000000045;
    printf("Reverse of no. is %d\n", reversDigits(num));
 
    return 0;
}


Java
// Java program to reverse digits of a number
 
class ReverseDigits
{
    /* Iterative function to reverse digits of num*/
    static int reversDigits(int num)
    {
        // Handling negative numbers
        boolean negativeFlag = false;
        if (num < 0)
        {
            negativeFlag = true;
            num = -num ;
        }
      
        int prev_rev_num = 0, rev_num = 0;
        while (num != 0)
        {
            int curr_digit = num%10;
      
            rev_num = (rev_num*10) + curr_digit;
      
            // checking if the reverse overflowed or not.
            // The values of (rev_num - curr_digit)/10 and
            // prev_rev_num must be same if there was no
            // problem.
            if ((rev_num - curr_digit)/10 != prev_rev_num)
            {
                System.out.println("WARNING OVERFLOWED!!!");
                return 0;
            }
      
            prev_rev_num = rev_num;
            num = num/10;
        }
      
        return (negativeFlag == true)? -rev_num : rev_num;
    }
     
    public static void main (String[] args)
    {
        int num = 12345;
        System.out.println("Reverse of no. is " + reversDigits(num));
      
        num = 1000000045;
        System.out.println("Reverse of no. is " + reversDigits(num));
    }
}


Python3
# Python program to reverse digits
# of a number
 
""" Iterative function to reverse
digits of num"""
def reversDigits(num):
 
    # Handling negative numbers
    negativeFlag = False
    if (num < 0):
     
        negativeFlag = True
        num = -num
     
 
    prev_rev_num = 0
    rev_num = 0
    while (num != 0):
     
        curr_digit = num % 10
 
        rev_num = (rev_num * 10) + curr_digit
 
        # checking if the reverse overflowed or not.
        # The values of (rev_num - curr_digit)/10 and
        # prev_rev_num must be same if there was no
        # problem.
        if (rev_num >= 2147483647 or
            rev_num <= -2147483648):
            rev_num = 0
        if ((rev_num - curr_digit) // 10 != prev_rev_num):
         
            print("WARNING OVERFLOWED!!!")
            return 0
         
 
        prev_rev_num = rev_num
        num = num //10
     
 
    return -rev_num if (negativeFlag == True) else rev_num
 
 
 
 
# Driver code
if __name__ =="__main__":
    num = 12345
    print("Reverse of no. is ",reversDigits(num))
 
    num = 1000000045
    print("Reverse of no. is ",reversDigits(num))
         
     
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# program to reverse digits
// of a number
using System;
 
class GFG
{
 
/* Iterative function to reverse
   digits of num*/
static int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num % 10;
 
        rev_num = (rev_num * 10) +
                   curr_digit;
 
        // checking if the reverse overflowed
        // or not. The values of (rev_num -
        // curr_digit)/10 and prev_rev_num must
        // be same if there was no problem.
        if ((rev_num - curr_digit) / 10 != prev_rev_num)
        {
            Console.WriteLine("WARNING OVERFLOWED!!!");
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num / 10;
    }
 
    return (negativeFlag == true) ?
                         -rev_num : rev_num;
}
 
// Driver Code
static public void Main ()
{
    int num = 12345;
    Console.WriteLine("Reverse of no. is " +
                         reversDigits(num));
 
    num = 1000000045;
    Console.WriteLine("Reverse of no. is " +
                         reversDigits(num));
}
}
 
// This code is contributed by ajit


输出:

6985

但是,如果数量很大,以至于反向溢出,则输出是一些垃圾值。如果我们使用输入为1000000045的任何大数字来运行上面的代码,则输出为某个垃圾值,例如1105032705或任何其他垃圾值。看到此输出。
如何处理溢出?
想法是存储和的先前值可以存储在变量中,该变量可以每次检查以查看反向是否溢出。
下面是处理这种情况的实现。

C++

// C++ program to reverse digits
// of a number
#include 
 
using namespace std;
 
/* Iterative function to reverse
digits of num*/
int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num % 10;
 
        rev_num = (rev_num * 10) + curr_digit;
 
        // checking if the reverse overflowed or not.
        // The values of (rev_num - curr_digit)/10 and
        // prev_rev_num must be same if there was no
        // problem.
        if ((rev_num - curr_digit) /
               10 != prev_rev_num)
        {
            cout << "WARNING OVERFLOWED!!!"
                 << endl;
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num / 10;
    }
 
    return (negativeFlag == true) ?
                         -rev_num : rev_num;
}
 
// Driver Code
int main()
{
    int num = 12345;
    cout << "Reverse of no. is "
         << reversDigits(num) << endl;
 
    num = 1000000045;
    cout << "Reverse of no. is "
         << reversDigits(num) << endl;
 
    return 0;
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)

C

// C program to reverse digits of a number
#include 
 
/* Iterative function to reverse digits of num*/
int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num%10;
 
        rev_num = (rev_num*10) + curr_digit;
 
        // checking if the reverse overflowed or not.
        // The values of (rev_num - curr_digit)/10 and
        // prev_rev_num must be same if there was no
        // problem.
        if ((rev_num - curr_digit)/10 != prev_rev_num)
        {
            printf("WARNING OVERFLOWED!!!\n");
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num/10;
    }
 
    return (negativeFlag == true)? -rev_num : rev_num;
}
 
/* Driver program to test reverse Digits */
int main()
{
    int num = 12345;
    printf("Reverse of no. is %d\n", reversDigits(num));
 
    num = 1000000045;
    printf("Reverse of no. is %d\n", reversDigits(num));
 
    return 0;
}

Java

// Java program to reverse digits of a number
 
class ReverseDigits
{
    /* Iterative function to reverse digits of num*/
    static int reversDigits(int num)
    {
        // Handling negative numbers
        boolean negativeFlag = false;
        if (num < 0)
        {
            negativeFlag = true;
            num = -num ;
        }
      
        int prev_rev_num = 0, rev_num = 0;
        while (num != 0)
        {
            int curr_digit = num%10;
      
            rev_num = (rev_num*10) + curr_digit;
      
            // checking if the reverse overflowed or not.
            // The values of (rev_num - curr_digit)/10 and
            // prev_rev_num must be same if there was no
            // problem.
            if ((rev_num - curr_digit)/10 != prev_rev_num)
            {
                System.out.println("WARNING OVERFLOWED!!!");
                return 0;
            }
      
            prev_rev_num = rev_num;
            num = num/10;
        }
      
        return (negativeFlag == true)? -rev_num : rev_num;
    }
     
    public static void main (String[] args)
    {
        int num = 12345;
        System.out.println("Reverse of no. is " + reversDigits(num));
      
        num = 1000000045;
        System.out.println("Reverse of no. is " + reversDigits(num));
    }
}

Python3

# Python program to reverse digits
# of a number
 
""" Iterative function to reverse
digits of num"""
def reversDigits(num):
 
    # Handling negative numbers
    negativeFlag = False
    if (num < 0):
     
        negativeFlag = True
        num = -num
     
 
    prev_rev_num = 0
    rev_num = 0
    while (num != 0):
     
        curr_digit = num % 10
 
        rev_num = (rev_num * 10) + curr_digit
 
        # checking if the reverse overflowed or not.
        # The values of (rev_num - curr_digit)/10 and
        # prev_rev_num must be same if there was no
        # problem.
        if (rev_num >= 2147483647 or
            rev_num <= -2147483648):
            rev_num = 0
        if ((rev_num - curr_digit) // 10 != prev_rev_num):
         
            print("WARNING OVERFLOWED!!!")
            return 0
         
 
        prev_rev_num = rev_num
        num = num //10
     
 
    return -rev_num if (negativeFlag == True) else rev_num
 
 
 
 
# Driver code
if __name__ =="__main__":
    num = 12345
    print("Reverse of no. is ",reversDigits(num))
 
    num = 1000000045
    print("Reverse of no. is ",reversDigits(num))
         
     
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)

C#

// C# program to reverse digits
// of a number
using System;
 
class GFG
{
 
/* Iterative function to reverse
   digits of num*/
static int reversDigits(int num)
{
    // Handling negative numbers
    bool negativeFlag = false;
    if (num < 0)
    {
        negativeFlag = true;
        num = -num ;
    }
 
    int prev_rev_num = 0, rev_num = 0;
    while (num != 0)
    {
        int curr_digit = num % 10;
 
        rev_num = (rev_num * 10) +
                   curr_digit;
 
        // checking if the reverse overflowed
        // or not. The values of (rev_num -
        // curr_digit)/10 and prev_rev_num must
        // be same if there was no problem.
        if ((rev_num - curr_digit) / 10 != prev_rev_num)
        {
            Console.WriteLine("WARNING OVERFLOWED!!!");
            return 0;
        }
 
        prev_rev_num = rev_num;
        num = num / 10;
    }
 
    return (negativeFlag == true) ?
                         -rev_num : rev_num;
}
 
// Driver Code
static public void Main ()
{
    int num = 12345;
    Console.WriteLine("Reverse of no. is " +
                         reversDigits(num));
 
    num = 1000000045;
    Console.WriteLine("Reverse of no. is " +
                         reversDigits(num));
}
}
 
// This code is contributed by ajit

输出:

Reverse of no. is 54321
WARNING OVERFLOWED!!!
Reverse of no. is 0