Created
December 3, 2018 07:03
-
-
Save XcqRomance/b0ed09b89973440d99e803bb250e3a8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
如何判断是否溢出? | |
其实看我上面的代码也可以看得出了,只要把这个式子反着推过来,再来看是否相等就行了。 | |
加法溢出判断:若c=a+b; c-a!=b则溢出;或者a, b>0, c<0溢出;或者a, b<0, c>0溢出; | |
减法溢出判断:若c=a-b; c+b!=a则溢出; | |
除法溢出判断:若b!=0 && a/b=c; b*c!=a则溢出 | |
乘法溢出判断:若c=a*b; a!=0 && c/a!=b则溢出 | |
*/ | |
// 颠倒整数 | |
int reverseInt(int x) { | |
int y = 0; | |
while (x) { | |
int temp = y; | |
y = y*10 + x%10; | |
if ((y - x%10)/10 != temp) { // 溢出 | |
return 0; | |
} | |
x /= 10; | |
} | |
return y; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment