不使用'/'进行除法

谁能告诉我不使用'/'来执行除法运算的有效方法。我可以log(n)使用类似于二进制搜索的方法逐步计算整数值。


115/3 

57 * 3 > 115

28 * 3 < 115

47 * 3 > 115

.

.

.

38 * 3 is quotient value .....

但是还有其他更有效的方法吗?


青春有我
浏览 597回答 3
3回答

一只名叫tom的猫

选项:根据您在小学学习的长除法算法编写自己的除法算法。取分母的-1的幂,然后乘以分子取分子和分母的对数,减去,然后将对数的底数提高到相同的幂我并不特别喜欢这样的问题,因为我们基本上是在寻找愚蠢的把戏,但事实确实如此。

凤凰求蛊

以下是不使用除法运算符对数字进行除法的Java代码。private static int binaryDivide(int dividend, int divisor) {&nbsp; &nbsp; int current = 1;&nbsp; &nbsp; int denom = divisor;&nbsp; &nbsp; // This step is required to find the biggest current number which can be&nbsp; &nbsp; // divided with the number safely.&nbsp; &nbsp; while (denom <= dividend) {&nbsp; &nbsp; &nbsp; &nbsp; current <<= 1;&nbsp; &nbsp; &nbsp; &nbsp; denom <<= 1;&nbsp; &nbsp; }&nbsp; &nbsp; // Since we may have increased the denomitor more than dividend&nbsp; &nbsp; // thus we need to go back one shift, and same would apply for current.&nbsp; &nbsp; denom >>= 1;&nbsp; &nbsp; current >>= 1;&nbsp; &nbsp; int answer = 0;&nbsp; &nbsp; // Now deal with the smaller number.&nbsp; &nbsp; while (current != 0) {&nbsp; &nbsp; &nbsp; &nbsp; if (dividend >= denom) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dividend -= denom;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer |= current;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; current >>= 1;&nbsp; &nbsp; &nbsp; &nbsp; denom >>= 1;&nbsp; &nbsp; }&nbsp; &nbsp; return answer;}
打开App,查看更多内容
随时随地看视频慕课网APP