The most efficient way to reverse a number

后端 未结 10 752

I am looking for an efficient algorithm to reverse a number, e.g.

Input: 3456789

Output: 9876543

In C++ there are p

相关标签:
10条回答
  • 2021-01-13 05:03

    Something like this will work:

    #include <iostream>
    
    int main()
    {
        long in = 3456789;
        long out = 0;
        while(in)
        {
            out *= 10;
            out += in % 10;
            in /= 10;
        }
        std::cout << out << std::endl;
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-13 05:03

    You may convert the number to string and then reverse the string with STL algorithms. Code below should work:

     long number = 123456789;
     stringstream ss;
     ss << number;
     string numberToStr = ss.str();
    
     std::reverse(numberToStr.begin(), numberToStr.end());
    
     cout << atol(numberToStr.c_str());
    

    You may need to include those relevant header files. I am not sure whether it is the most efficient way, but STL algorithms are generally very efficient.

    0 讨论(0)
  • 2021-01-13 05:05
     //Recursive method to find the reverse of a number  
     #include <bits/stdc++.h> 
        using namespace std; 
        int reversDigits(int num) 
        { 
        static int rev_num = 0; 
        static int base_pos = 1; 
        if(num > 0) 
        { 
            reversDigits(num/10); 
            rev_num += (num%10)*base_pos; 
            base_pos *= 10; 
        } 
        return rev_num; 
        } 
        int main() 
        { 
            int num = 4562; 
            cout << "Reverse  " << reversDigits(num); 
        } ``
    
    0 讨论(0)
  • 2021-01-13 05:10
    #include <stdio.h>
    unsigned int reverse(unsigned int val)
    {
     unsigned int retval = 0;
    
     while( val > 0)
     {
         retval  = 10*retval + val%10;
         val     /= 10;
     }
     printf("returning - %d", retval);
     return retval;
    }
    
    
    int main()
    {
        reverse(123);
    }
    
    0 讨论(0)
  • 2021-01-13 05:13
    static public int getReverseInt(int value) {
        int resultNumber = 0;
        for (int i = value; i != 0;) {
            int d = i / 10;
            resultNumber = (resultNumber - d) * 10 + i;
            i = d;
        }
        return resultNumber;
    }
    

    I think this will be the fastest possible method without using asm. Note that d*10 + i is equivalent to i%10 but much faster since modulo is around 10 times slower than multiplication.
    I tested it and it is about 25 % faster than other answers.

    0 讨论(0)
  • 2021-01-13 05:17

    This is the easiest one:

    #include<iostream>
    using namespace std;
    
    int main()
    {
    int number, reversed=0;
    cout<<"Input a number to Reverse: ";
    cin>>number;
        while(number!=0)
      {
        reversed= reversed*10;
        reversed=reversed+number%10;
        number=number/10;
      }
    cout<<"Reversed number is: "<<reversed<<endl;
    
    }
    
    0 讨论(0)
提交回复
热议问题