The most efficient way to reverse a number

后端 未结 10 839

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 
    
    int main()
    {
        long in = 3456789;
        long out = 0;
        while(in)
        {
            out *= 10;
            out += in % 10;
            in /= 10;
        }
        std::cout << out << std::endl;
        return 0;
    }
    

提交回复
热议问题