3 回答
TA贡献1906条经验 获得超3个赞
选项:
根据您在小学学习的长除法算法编写自己的除法算法。
取分母的-1的幂,然后乘以分子
取分子和分母的对数,减去,然后将对数的底数提高到相同的幂
我并不特别喜欢这样的问题,因为我们基本上是在寻找愚蠢的把戏,但事实确实如此。
TA贡献1825条经验 获得超4个赞
以下是不使用除法运算符对数字进行除法的Java代码。
private static int binaryDivide(int dividend, int divisor) {
int current = 1;
int denom = divisor;
// This step is required to find the biggest current number which can be
// divided with the number safely.
while (denom <= dividend) {
current <<= 1;
denom <<= 1;
}
// Since we may have increased the denomitor more than dividend
// thus we need to go back one shift, and same would apply for current.
denom >>= 1;
current >>= 1;
int answer = 0;
// Now deal with the smaller number.
while (current != 0) {
if (dividend >= denom) {
dividend -= denom;
answer |= current;
}
current >>= 1;
denom >>= 1;
}
return answer;
}
添加回答
举报