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. 思路: ...
随机推荐
- php构造函数construct用法注意事项
<?php class A { //特别注意,这里的下划线为两个 function __construct() { echo "I am the constructor of A.&l ...
- linux ln命令 创建链接(快捷方式)
命令格式: ln -s 目标地址 链接名称 # 假设/home目录下有wuyou文件夹,你要在当前目录创建一个链接指向它 $ ln -s /home/wuyou wuyou_link
- linux Ubuntu12 设置root用户登录图形界面
Ubuntu 12.04默认是不允许root登录的,在登录窗口只能看到普通用户和访客登录.以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后,修改系统配置文件需要切换到超级用户模式,在终端 ...
- 【疯狂Java学习笔记】【理解面向对象】
[学习笔记]1.Java语言是纯粹的面向对象语言,这体现在Java完全支持面向对象的三大基本特征:封装.继承.多态.抽象也是面向对象的重要组成部分,不过它不是面向对象的特征之一,因为所有的编程语言都需 ...
- 屏蔽QQ聊天对话框中的广告
原文地址: 怎么在QQ聊天对话框中屏蔽广告_百度经验 http://jingyan.baidu.com/article/48a42057ca12c1a924250402.html QQ已经成为 ...
- *MySQL卸载之后无法重装,卡在Apply security settings:Error Nr.1045
- 【算法】 输入n 输出一个n*n的zigzag矩阵 利用c++实现
int main() { int N; cin>>N; int **a = new int *[N]; ) ;//如果没有申请到空间 ;i<N;i++) { a[i]= new in ...
- 开源布局控件 WeifenLuo.WinFormsUI.Docking.dll使用
WeifenLuo.WinFormsUI.Docking是一个很强大的界面布局控件,可以保存自定义的布局为XML文件,可以加载XML配置文件.! 先看一下效果 使用说明: 1.新建一个WinForm程 ...
- 【TopCoder】SRM 680 DIV 2
1. BearPair之bigDistance1.1 题目概述在 <= 50的字符串中找位置i,j 满足(1) s[i] != s[j];(2) abs(i-j)尽可能大.若不存在返回-1, 否 ...
- Java数据库增删改查
数据库为MySQL数据库,Oracle数据库类似: create database db_test;--创建数据库 ';--创建用户 grant all privileges on db_test.* ...