The most efficient way to reverse a number

后端 未结 10 845

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

    int ans=0;
    int rev(int n)
    {
      ans=(ans+(n%10))*10; // using recursive function to reverse a number;
      if(n>9) 
        rev(n/10);
    }
    
    int main()
    {
      int m=rev(456123); // m=32
      return 0;
    }
    

提交回复
热议问题