题目描述:

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

题目来源: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. Oracle JDBC 连接卡死后 Connection Reset解决过程

    https://www.cnblogs.com/pthwang/p/8949445.html

  2. android 用webView作为编辑器 各种问题

    1.首先我要说明一下为什么要写这个博客,因为公司最近需要一个自定义的编辑器,苦于没有思路在网上找了好久,看到了好多android实现的编辑器(其实也就那么几个并不多),公司需求和网页端同步共享创建的文 ...

  3. Linux 批量替换的一种实现方式

    替换某目录下所有文件中的某个字符: sed -i 's/origin_str/new_str/g' `grep origin_str -rl ./` origin_str:被替换的字符串: new_s ...

  4. 【WPF】ComboBox:根据绑定选取、设置固定集合中的值

    问题场景 我有一个对象,里面有一个属性叫Limit,int类型.虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5.10.15.20. 所以ComboBox绑定不是绑定常 ...

  5. TCP/IP状态详解

    今天犯懒了,本来自己也做了一些相应的笔记,但是发现这篇写的更好一些,简单易懂,而且有图有真相,为了方便以后查看,在此转载了,在此基础上加了自己的笔记                 TCP正常建立和关 ...

  6. NHibernate学习系列一

    NHibernate是一个面向.NET环境的对象/关系数据库映射工具.对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映 ...

  7. samba服务器的搭建和配置

    案例: 公司有两个部门, sales / market . 分别有成员 jack / tom 和 zhang / shen . 公司需求是这样的, 本部门资料禁止其他部门访问, 本部门成员之间不能干扰 ...

  8. Grunt学习笔记【8】---- grunt-angular-templates插件详解

    本文主要讲如何用Grunt打包AngularJS的模板HTML. 一 说明 AngularJS中使用单独HTML模板文件的地方非常多,例如:自定义指令.ng-include.templateUrl等. ...

  9. 使用qt+ros调用摄像头遇到的问题

    当使用摄像头遇到如下问题:[usb_cam-1] process has died [pid 12288, exit code 127, cmd /opt/ros/indigo/lib/usb_cam ...

  10. Warning: Cannot send session cookie – headers already sent…

    相信大多数人在写PHP代码的时候,都遇到过类似 "Warning: Cannot send session cookie – headers already sent…“或者”Cannot ...