The most efficient way to reverse a number

后端 未结 10 757

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:05

     //Recursive method to find the reverse of a number  
     #include  
        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); 
        } ``
    

提交回复
热议问题