class Solution { public: int divide(int dividend, int divisor) { if (divisor == 0 || (dividend == INT_MIN && divisor == -1)) return INT_MAX; long m = (dividend); long n = (divisor); m = abs(m); n = abs(n); int sign = (dividend>0)^(divisor>0)?-1:1; int p = n;//此处不能用int int q = 1; long ans = 0 ; while(m>=n){ while(m>=(p<<1)){//如果p=1是int m=INT_MAX 那么这个循环永远无法推出 p = p << 1; q = q << 1; } ans += q; m -= p; p = n; q = 1; } ans = sign>0?ans:-ans; if(ans>INT_MAX||ans<INT_MIN){ return INT_MAX; } return ans; } };