为了账号安全,请及时绑定邮箱和手机立即绑定

不使用'/'进行除法

不使用'/'进行除法

青春有我 2019-11-11 14:01:05
谁能告诉我不使用'/'来执行除法运算的有效方法。我可以log(n)使用类似于二进制搜索的方法逐步计算整数值。115/3 57 * 3 > 11528 * 3 < 11547 * 3 > 115...38 * 3 is quotient value .....但是还有其他更有效的方法吗?
查看完整描述

3 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超2个赞

选项:

  • 根据您在小学学习的长除法算法编写自己的除法算法。

  • 取分母的-1的幂,然后乘以分子

  • 取分子和分母的对数,减去,然后将对数的底数提高到相同的幂

我并不特别喜欢这样的问题,因为我们基本上是在寻找愚蠢的把戏,但事实确实如此。


查看完整回答
反对 回复 2019-11-11
?
凤凰求蛊

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;

}


查看完整回答
反对 回复 2019-11-11
  • 3 回答
  • 0 关注
  • 591 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信