【题目描述】Divide two integers without using multiplication, division and mod operator.
不使用乘,除,模符号作除法运算
【解题思路】每一个数都可以用2进制表示,利用这一点通过位运算完成
【考查内容】模拟
class Solution {
public:
int divide(int dividend, int divisor) {
long long a = abs((long long)dividend);
long long b = abs((long long)divisor);
int finalcount=0;
while(a >= b){
int count = 1;
long sum = b;
while(sum + sum <= a){
sum += sum;
count += count;
}
a -= sum;
finalcount += count;
};
return (dividend>>31 ^ divisor>>31)? -finalcount:finalcount;
}
};