题目描述:

不用 '*' '/' 和 '%' 运算实现两个整数的除法

题目来源:http://oj.leetcode.com/problems/divide-two-integers/

题目分析:

例如 16 / 3,可以按照3的倍数来操作。 3 * 1 = 3, 3 * 2 = 6, 3 * 2 * 2 = 12 ...,而2 * 2等可以用位运算方式表示。

那么有16 - 3 = 13, 13 - 3 * 2 = 7, 而7 < 3 * 2 *2。可以迭代处理,再计算剩下的 7 / 3,依次进行。

位运算的典型应用

示例代码:

int divide(int dividend, int divisor) {
long long a = abs((double)dividend), b = abs((double)divisor), ret = ;
while(a >= b) {
for(long long c = b, i = ; a >= c; ++i, c <<= ) {
a -= c;
ret += << i;
}
} return ((dividend ^ divisor) >> ) ? -ret : ret;
}

Divide Two Integers-不用'/' '*' '%'操作实现整数的除法的更多相关文章

  1. 【Leetcode】 - Divide Two Integers 位运算实现整数除法

    实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...

  2. [leetcode]29. Divide Two Integers不用除法实现除法

    思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...

  3. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  4. [leetcode]29. Divide Two Integers两整数相除

      Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...

  5. 【LeetCode每天一题】Divide Two Integers(两整数相除)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. Leetcode Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...

  8. LeetCode(29)Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  9. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

随机推荐

  1. Go开发常见陷阱

    Go作为一种简便灵巧的语言,深受开发者的喜爱.但对于初学者来说,要想轻松驾驭它,还得做好细节学习工作. 初学者应该注意的地方: 大括号不能独立成行. 未使用变量错误——对于全局变量和函数参数变量,是可 ...

  2. 如何创建虚拟硬盘 + os 读取硬盘参数代码

    [0]README 0.1) 本文旨在演示如何利用 bximage 创建虚拟硬盘: 0.2) 利用 os 读取硬盘参数, source code from orange's implemention ...

  3. Python学习总结之四 -- 这就是Python的字典

    字典原来是这么回事儿 Python学习到现在,我们已经知道,如果想将值分组到结构中,并且通过编号对其进行引用,列表就可以派上用场.不过,今天,我们将学到一种通过名字引用值的数据结构,应该知道这种数据类 ...

  4. JQuery 获取URL中传递的参数

    该方法基于JQuery,然后给方法传递URL中的参数名,返回参数值 (function($){    $.getUrlParam = function(name){        var reg = ...

  5. 【BZOJ1316】树上的询问 点分治+set

    [BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...

  6. 九度OJ 1011:最大连续子序列 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5615 解决:2668 题目描述:     给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, N ...

  7. Consumer Group Example

    面向kafka编程 Consumer Group Example https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Ex ...

  8. POJ 3714 Raid 近期对点题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  9. 2018年东北农业大学春季校赛 E wyh的集合 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/93/F 思路 其实容易知道在两个不同集合里 假设元素个数 分别为 a b 然后对于第一个集合里的每一个元素 都可以 ...

  10. 经典数学问题<手电过河问题>的动态解法--问题规模扩展至任意大小

    非常有趣的一件事是今天在TopCoder的1000分题里面发现了这道经典数学问题. Notes           -                   In an optimal solution ...