题目描述:

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

题目来源: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. HDOJ 题目3564 Another LIS(线段树单点更新,LIS)

    Another LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  2. Mybatis之基本简介

    一.Mybatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的JDBC代码.手工设置参数和结果集重获.MyBatis 只使用简单的XML ...

  3. c#数组的count()和length的区别

    C# 数组中 Length 表示数组项的个数,是个属性. 而 Count() 也是表示项的个数,是个方法,它的值和 Length 一样.但实际上严格地说 Count() 不是数组的内容,而是 IEnu ...

  4. input file 选择Excel文件 相关操作

    1.HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFo ...

  5. iOS与H5交互及UIWebView缓存

    iOS原生App与H5页面交互笔记 最近在做一个项目用到了原生App与H5交互,之前有做过简单的H5页面直接调用原生方法的例子,就是利用UIWebView中的代理方法 //webview每次加载之前都 ...

  6. Drupal 安装过程

    php.ini 文件 https://drupal.stackexchange.com/questions/164172/problem-installing-in-local-the-transla ...

  7. Algorithm: dynamic programming

    1. Longest Increasing Subsequence (LIS) problem unsorted array, calculate out the maximum length of ...

  8. Android Weekly Notes Issue #321

    Android Weekly Issue #321 August 5th, 2018. Android Weekly Issue #321 本期内容包括: 开源项目Plaid的改版; 使用Tensor ...

  9. Spring Boot2.0之全局捕获异常

    全局捕获异常,很明显的错误404返回给客户,很不好呀.整个web请求项目全局捕获异常,比如空指针直接返回给客户啊,那多操蛋呀~ 看这几个常用的注解: @ExceptionHandler 表示拦截异常 ...

  10. consider increasing the maximum size of the cache.

    虚拟机上搭建jenkins,出现unable to free [10] percent of the cache for Context [/jenkins] 提示让我加大缓存 consider in ...