#include<bits/stdc++.h>
using namespace std; int divide(int dividend, int divisor) {
long long n = dividend, m = divisor;
// determine sign of the quotient
int sign = n < ^ m < ? - : ; // remove sign of operands
n = abs(n), m = abs(m); // q stores the quotient in computation
long long q = ; // test down from the highest bit
// accumulate the tentative value for valid bits
for (long long t = , i = ; i >= ; i--)
if (t + (m << i) <= n)
t += m << i, q |= << i; // assign back the sign
if (sign < ) q = -q; // check for overflow and return
return q >= INT_MAX || q < INT_MIN ? INT_MAX : q;
} int main() { cout << divide(-, ) << endl;
return ;
}

algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operation)的更多相关文章

  1. Divide two integers without using multiplication, division and mod operator.

    描述 不能使用乘法.除法和取模(mod)等运算,除开两个数得到结果,如果内存溢出则返回Integer类型的最大值.解释一下就是:输入两个数,第一个数是被除数dividend,第二个是除数divisor ...

  2. leetcode 29-> Divide Two Integers without using multiplication, division and mod operator

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...

  3. Divide Two Integers leetcode

    题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...

  4. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

    1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...

  5. [LeetCode] Divide Two Integers 两数相除

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

  6. Leetcode Divide Two Integers

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

  7. leetcode-【中等题】Divide Two Integers

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

  8. [LintCode] Divide Two Integers 两数相除

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

  9. 62. Divide Two Integers

    Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...

随机推荐

  1. JSP中脚本、声明和表达式的本质区别

     JSP脚本元素 使用JSP脚本元素可以将Java代码嵌入到JSP页面里,这些Java代码将出现在由当前JSP页面生成的Servlet中,使JSP将静态内容与动态内容分离出来.脚本元素包含:  1. ...

  2. linux Ubuntu12 设置root用户登录图形界面

    Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...

  3. 安卓从业者应该关注:Android 6.0的运行时权限

    Android 6.0,代号棉花糖,自发布伊始,其主要的特征运行时权限就很受关注.因为这一特征不仅改善了用户对于应用的使用体验,还使得应用开发者在实践开发中需要做出改变. 没有深入了解运行时权限的开发 ...

  4. WampServer安装图解教程

    WampServer中文安装教程_百度经验 http://jingyan.baidu.com/article/0bc808fc9d66f41bd485b925.html WampServer是国外知名 ...

  5. 174. Dungeon Game

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  6. Struts2国际化文件乱码解决

    方法1:使用native2ascii进行编码转换 代码如下: native2ascii -encoding UTF-8 GlobalMessages.properties NewGlobalMessa ...

  7. POJ 3096 Surprising Strings(STL map string set vector)

    题目:http://poj.org/problem?id=3096 题意:给定一个字符串S,从中找出所有有两个字符组成的子串,每当组成子串的字符之间隔着n字符时,如果没有相同的子串出现,则输出 &qu ...

  8. C语言中的位拷贝与值拷贝浅谈(转载)

    注:C语言实现的PHP变量的赋值过程中,就涉及到了 深拷贝和浅拷贝 位拷贝拷贝的是地址(也叫浅拷贝),而值拷贝则拷贝的是内容(深拷贝).深拷贝和浅拷贝可以简单理解为:如果一个类拥有资源,当这个类的对象 ...

  9. bzoj1293

    简易贪心+heap 注意要用链表 type link=^node;      node=record        loc:longint;        next:link;      end;   ...

  10. 8.11-8.16:usaco

    summary:57 bzoj1741:裸二分图最大匹配 #include<cstdio> #include<cstring> #include<iostream> ...