G****A 发帖数: 4160 | 1 网上有原题:
Divide two integers without using multiplication, division and mod operator.
目前有三个方向:
1, 用减法做除法
2, binary search
3, bit manipulation
谁能提供一下2、3的思路。谢谢 | c****p 发帖数: 6474 | 2 大意是这样。。代码没检查。
a/b:
if a
if a==b return 1;
int shifts = 0;
while(b
int results = 0;
while(shifts>=0)
{
if(a>b)
{a-=b;
results|=1;
if(a==0)
{
results <<= shifts;
break;
}
}
b>>=1;
shifts--;
results<<=1;
}
return results;
operator.
【在 G****A 的大作中提到】 : 网上有原题: : Divide two integers without using multiplication, division and mod operator. : 目前有三个方向: : 1, 用减法做除法 : 2, binary search : 3, bit manipulation : 谁能提供一下2、3的思路。谢谢
| s**********r 发帖数: 8153 | 3 都说说呗,
怎么用减法做除法啊?
我只看到了bit的那个。
operator.
【在 G****A 的大作中提到】 : 网上有原题: : Divide two integers without using multiplication, division and mod operator. : 目前有三个方向: : 1, 用减法做除法 : 2, binary search : 3, bit manipulation : 谁能提供一下2、3的思路。谢谢
| j********x 发帖数: 2330 | 4 减法就是不停地减
记一下减掉的b的个数,知道为负数,这个减掉的个数就是商。。。 | n*******w 发帖数: 687 | 5 bit操作应该也类似bs
模拟手算二进制除法就行了。
operator.
【在 G****A 的大作中提到】 : 网上有原题: : Divide two integers without using multiplication, division and mod operator. : 目前有三个方向: : 1, 用减法做除法 : 2, binary search : 3, bit manipulation : 谁能提供一下2、3的思路。谢谢
| c*****a 发帖数: 808 | | z*******o 发帖数: 4773 | 7 这...
【在 c*****a 的大作中提到】 : a/b = log(a) - log(b)
|
|