Divide Two Integers(模拟计算机除法)
Divide two integers without using multiplication, division and mod operator.
由于不能用乘号,除号,和取余。那么一个数除另外一个数,如a/b,实际含义是a中有多少个b,我们可以多次计算a-b,并更新a,最后a-b<0说明循环结束,循环的次数也即结果。
但是上面这种方法超时,如2147483647/1,那么循环次数为2147483647次。
所以考虑二分法来减少循环的次数。而且乘2,相对于左移一位,没有用到乘号。
代码:
class Solution {
private:
int res;
public:
int solve(long long dividend, long long divisor){
long long temp=;
while (divisor<=dividend)
{
divisor=divisor<<;
temp=temp<<;
}
divisor=divisor>>;
temp=temp>>;
res+=temp; return dividend-divisor; }
int divide(int dividend, int divisor) {
res=;
bool fu=false;
if(divisor==) return -;
if(divisor==) return dividend; long long my_dividend=(long long)dividend;
long long my_divisor=(long long)divisor;
if(my_dividend<&&my_divisor<){
my_dividend=-my_dividend;
my_divisor=-my_divisor;
fu=false;
}
if(my_dividend<) {my_dividend=-my_dividend;fu=true;}
if(my_divisor<) {my_divisor=-my_divisor;fu=true;} while (((my_dividend=solve(my_dividend,my_divisor))-my_divisor)>=); if(fu) res=-res;
return res;
}
};
int main()
{
freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
Solution so;
int a=-;
int b=-;
cout<<so.divide(a,b)<<endl;
return ;
}
Divide Two Integers(模拟计算机除法)的更多相关文章
- [leetcode]29. Divide Two Integers不用除法实现除法
思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...
- [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. If it is overflow, r ...
- LeetCode29 Divide Two Integers
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [Math]Divide Two Integers
otal Accepted: 54356 Total Submissions: 357733 Difficulty: Medium Divide two integers without using ...
- [Leetcode][Python]29: Divide Two Integers
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...
- leetcode第28题--Divide Two Integers
Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...
- 【一天一道LeetCode】#29. Divide Two Integers
一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...
随机推荐
- shutil模块 + shelve模块 二合一版
其他的看我前面的博客 import shutil # 将文件内容拷贝到另一个文件with open('old.xml','r') as read_f,open('new.xml', 'w') as w ...
- CCF|跳一跳
import java.util.Scanner; public class Main { public static void main (String[] args) { Scanner scan ...
- Java字符串操作方法集
常用Java字符串操作方法 String s="Hello" String s2="World" 操作 方法 使用方法 结果 字符串截取 substring ...
- JVM最多能创建多少个线程: unable to create new native thread
转载自:http://www.rigongyizu.com/jvm-max-threads/ 有应用报出这样的异常“java.lang.OutOfMemoryError: unable to crea ...
- div根据鼠标的移入移除显示隐藏
onmouseout 是把div当成一个对象,div里面包含的元素当成别的对象,所以移动的时候,会隐藏,达不到我们预期的效果. onmouseleave 就是把整个div当成一个对象. 大家可以去试 ...
- 【译】x86程序员手册38-10.2实在址模式下的软件初始化
10.2 Software Initialization for Real-Address Mode 实地址模式的软件初始化 In real-address mode a few structur ...
- JDK1.8中的Stream详解
Stream简介 Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 XML ...
- arp - Linux的ARP核心模块
描述 这个核心协议模块实现RFC826中定义的 Address Resolution Protocol [译注:即TCP/IP的第三层到第一层的地址转换协议],用于在直接相连的网络中换第二层硬件地址和 ...
- python的logging的简单使用
用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...
- ActiveX控件获取不到对象属性或者方法的原因分析
1.找不到调用的DLL或程序: 2.调用控件方法名称,与定义的函数名称不符合: 3.如果是网站网页调用ActiveX,检查控件是否添加安全对象: 4.如果是网站网页调用ActiveX,检查网页是否加入 ...