int div1(int a,int b)
{
int up=a;
int down=b;
int c=0;
int neg=0;
if(a>0)
{
up=~a+1;
}
if(b>0)
{
down=~b+1;
}
//为符合边界条件,全部取负值
while(up<=down)
{int count1 =-1;
while(down>0xc0000000&&up<=(down+down))
{
down+=down;
count1+=count1;
}
up=up-down;
if(b>0)
{
down=~b+1;
}
else{down=b;}//算完一半后,根据正负重新赋值
c+=count1;
}
//如果最慢的话则每次正好减去一半 假设去除n次 则Max=2^(n1) Max/2=2^(n2).....Max/2^m=2^(nm)
//所以时间长度为 n1+n2+....nm==log2(Max)+....log2(Max/2^m)==log2(Max*Max/2*....Max/2^nm)==log2(n^m/2^(0+m)*m/2)显然符合log2n的计算
//符号取反
if((a<0&&b<0)||(a>0&&b>0)){
c=~c+1;
}
return c;
}
int divide(int a, int b){
if(a==-2147483648&&b==-1)//边界条件
{return 2147483647;}
int c =div1(a,b);
return c;
}
01-18
1510
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)