Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
#include <iostream>
using namespace std;#define INT_MAX (32767)
#define INT_MIN (-INT_MAX-1)
class solution
{
public:
int reverse(int&num)
{
long long res = 0;
while (num)
{
res = res * 10 + num % 10;
num = num / 10;
}
return (res<INT_MIN || res>INT_MAX) ? 0 : res;
}
};
int main()
{
int num=0;
int re=0;
cout << "please input integernumber:" << endl;
cin >> num;
solution s;
re=s.reverse(num);
cout << re;
}