Reverse Integer
TotalAccepted: 73083 TotalSubmissions: 281068My Submissions
Question Solution
Reverse digits of an integer.
Example1: x = 123,return 321
Example2: x = -123, return -321
本题就是将int翻转下,有正负。唯一的坑在于int翻转之后可能超过int的范围,所以要用double来定义,同时要判断是否超出范围,超出直接返回0即可。
Ac代码源码(有main()):
#include <iostream>
using namespace std;
int f(int x)
{
intmax =0x7fffffff;
intmin= 0x80000000;
doubley=0;
while(x)
{
y=y*10+x%10;
x/=10;
}
if(y>max||y<min)
y=0;
returny;
}
int main()
{
intn;
while(cin>>n)
cout<<f(n)<<endl;
return0;
}