#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. HDU1569+最大点权集

    /* 最大点权独立集=总权值-最小点权覆盖集 最大点权独立集=最大流 最小点权覆盖集=最小割 题意: 给你一个m*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格 ...

  2. 1.Getting Started with ASP.NET MVC 5

    Getting Started Start by installing and running Visual Studio Express 2013 for Web or Visual Studio ...

  3. c++ string assign =

    C++ string类的成员函数,用于拷贝.赋值操作,它们允许我们顺次地把一个string 对象的部分内容拷贝到另一个string 对象上. string &operator=(const s ...

  4. ANDROID_MARS学习笔记_S01_004dpi、dp(dip)及计算

    一.dpi.dp介绍 sp会随着用户在手机中设置字体大小而改变,而dp不会 二.1.dpsp_layout.xml <?xml version="1.0" encoding= ...

  5. MSSQLServer基础04(常用函数)

    类型转换函数 CAST ( expression AS data_type) CONVERT ( data_type, expression,[style]) 对日期的转换.转换成各种国家格式的日期. ...

  6. 安装 ArcGIS Runtime SDK for Android

    ArcGIS for Android 开发:Android 平台搭建 - liyong20080101的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/liyong ...

  7. Oracl 动态执行表不可访问,本会话的自动统计被禁止

    oracle ---建立SQL窗体 写入 select * from tableA; 弹出错误窗口 : 动态执行表不可访问,本会话的自动统计被禁止.在执行菜单里你可以禁止统计,或在v$session, ...

  8. c# 可访问性级别

    使用访问修饰符 public.protected.internal 或 private 可以为成员指定以下声明的访问级别之一.   声明的可访问性 含义 public 访问不受限制. protecte ...

  9. P44、面试题4:替换空格

    题目:请实现一个函数,把字符串中的每个空格替换成“%20”.例如输入“We are happy.”,则输出“We%20are%20happy.”. 如果用java string类中提供的replace ...

  10. 在XML里的XSD和DTD以及standalone的使用

    有关XML结构中的XSD和DTD以及standalone的使用 XmlDeclaration declare= document.CreateXmlDeclaration("1.0" ...