Description:
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
分析:本题就是简单的把一个数倒过来,注意Note里面的规定,倒过来的数不能超出范围,附上C题解:
/*int reverse(int x) {
int c = x>0?x:-x;
long long int b=0;
for(int i=0;c>0;i++)
{
b=b*10+c%10;
c/=10;
}
if(b>pow(2,31)-1||b<-pow(2,31))
return 0;
else
return x>0?b:-b;
}题中正确的提交格式
*/
#include<stdio.h>
#include<math.h>
int main()
{
int x;
scanf("%d",&x);
int c = x>0?x:-x;
long long int b=0;
for(int i=0;c>0;i++)
{
b=b*10+c%10;
c/=10;
}
if(b>pow(2,31)-1||b<-pow(2,31))
printf("0");
else
printf("%lld",x>0?b:-b);
}//正常测试代码