algorithm@ Divide two integers without using multiplication, division and mod operator. (Bit Operation)
#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)的更多相关文章
- Divide two integers without using multiplication, division and mod operator.
描述 不能使用乘法.除法和取模(mod)等运算,除开两个数得到结果,如果内存溢出则返回Integer类型的最大值.解释一下就是:输入两个数,第一个数是被除数dividend,第二个是除数divisor ...
- leetcode 29-> Divide Two Integers without using multiplication, division and mod operator
class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...
- Divide Two Integers leetcode
题目:Divide Two Integers Divide two integers without using multiplication, division and mod operator. ...
- 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 ...
- [LeetCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- Leetcode Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 不用乘.除.求余操作,返回两整数相除的结果,结 ...
- leetcode-【中等题】Divide Two Integers
题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...
- [LintCode] Divide Two Integers 两数相除
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 62. Divide Two Integers
Divide Two Integers Divide two integers without using multiplication, division and mod operator. 思路: ...
随机推荐
- C++ 中判断非空的错误指针
最近在写网络上的东西,程序经过长时间的运行,会出现崩溃的问题,经过DUMP文件的查看,发现在recv的地方接收返回值的时候,数据的长度异常的大差不多16亿多字节.而查看分配后的char指针显示为错误的 ...
- URAL 1009 K-based numbers(DP递推)
点我看题目 题意 : K进制的N位数,不能有前导零,这N位数不能有连续的两个0在里边,问满足上述条件的数有多少个. 思路 : ch[i]代表着K进制的 i 位数,不含两个连续的0的个数. 当第 i 位 ...
- ExtJS4 MVC开发教程:搭建开发环境
原文地址:http://www.lihuai.net/qianduan/extjs/864.html 博主系列教程:http://www.lihuai.net/qianduan/extjs 在所有的J ...
- HTTPS访问:weblogic下配置SSL
进入Weblogic安装路径下的JDK安装目录bin文件下,通过keytool工具生成密钥对(标识密钥库) 输入命令,生成密钥 keytool.exe -genkey -v -alias weblog ...
- window下编译ffmpeg 比较简单
网上关于编译ffmpeg的帖子很多,我也尝试了很多次,但是很多都过不了,一部分原因是版本问题,还有就是有的路劲没说的太明白导致的,经过一天的摸索,最终编译好了,下面把编译方式写下来,希望对看到帖子的人 ...
- 创业公司Playcafe关门大吉 创始人总结10大失败教训
导读:互联网电视游戏网站PlayCafe的创始人马克·高登森(Mark Goldenson)日前撰文,总结了自己创业失败的十个教训.以下为文章主要内容: 一年半前,我与公司联合创始人戴维·奈格(Dev ...
- minitools
1.android 2.linux 3.luoji 4.windows CE ----
- Rails中的MIME类型
layout title date comments categories post rails的中的MIME类型 2014-09-08 21:40 true ruby Rails开发中经常使用不同的 ...
- bzoj3156
斜率优化dp,比较裸 注意int64的运算 ..] of int64; i,n,h,t:longint; x,y,z:int64; function g(j,k:int64):doub ...
- IE的体系和webrowser
IE的体系 WebBrowser Host首先,必须有COM的基础知识,因为IE本身就是COM技术的典型应用.我们看到最上层是WebBrowser的宿主(Host),也就是任何你想重用(ReUse)w ...