int divide(int x, int y) {
typedef long long LL;
vector<LL> exp;
bool is_fu = false;
if(x < 0 && y > 0 || x > 0 && y < 0) is_fu = true;
LL a = abs((LL)x),b = abs((LL)y);
for(LL i = b;i <= a;i = i + i) exp.push_back(i);
LL res = 0;
for(int i = exp.size() - 1;i >= 0; i --) {
if(a >= exp[i]) {
a -= exp[i];
res += (LL)1 << i;
}
}
if(is_fu) res = -res;
if(res > INT_MAX) res = INT_MAX;
return res;
}
两个int范围的数相除的结果,只有一种情况会超出int的表示范围。
-(2^31 - 1) 除以 (-1) = 2 ^ 31 - 1
超出了int的范围
对这种情况进行特判