Skip to content

Instantly share code, notes, and snippets.

@XcqRomance
Created December 3, 2018 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XcqRomance/b0ed09b89973440d99e803bb250e3a8f to your computer and use it in GitHub Desktop.
Save XcqRomance/b0ed09b89973440d99e803bb250e3a8f to your computer and use it in GitHub Desktop.
/*
如何判断是否溢出?
  其实看我上面的代码也可以看得出了,只要把这个式子反着推过来,再来看是否相等就行了。
加法溢出判断:若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