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 ...
随机推荐
- 7.JAVA-类继承、覆写、final关键字
1.JAVA继承-extends 在java中,要想实现继承则使用extends关键字. 一般子类被称为派生类,父类称为基类(super) extends需要注意的地方: java不允许多重继承(一个 ...
- 将call/apply方法应用于其他对象上的几种方法
在处理类数组中,发现了两种将数组方法应用于类数组的方法,现将call/apply的常用方式总结一下. 一.当做函数调用 function print_vars(var1,var2){ console. ...
- flex弹性布局操练1
实现弹性布局之前常用浮动,相对定位和绝对定位等,但是现在好了,随着flex的兴起,方便了很多,而且也符合未来响应式布局的方向. 理论的东西可参考css3手册,这里专注实操. 一:单个元素 <di ...
- Scala基础篇-01变量,数据类型
一.Scala变量 共有3种变量修饰符: val: 可变 var: 不可变 lazy val: 第一次应用时才计算 二.Scala的数据类型 1)类型不匹配:高精度数值赋给低精度数据类型. 2)Uni ...
- Winform webbrowser 隐藏 html 元素
目的:用webbrowser打开网页,并隐藏网页上某个html元素 1.如果已知元素ID,比较好办 直接使用webbrowser1.Document.getElementById("id&q ...
- windows ubuntu bcdeditor
双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...
- js 删除数组中某一项的几种方法总结
第一种:改变原数组 借用原生数组方法:splice(index,len,[item]) 剪接 借用原生对象方法:delete array[index] + array.slice(0, index) ...
- caffe数据读取
caffe的数据读取分为lmdb和 待清理,包括fast 这个一系列是怎么转换成lmdb数据的
- C# defult关键字
一.问题 今天写一个函数提示用defult,因为第一次用记录一下 public static T GetConfig<T>(string strConfig) { try { return ...
- 计算机中的CPU
今天写一下计算机中最核心的一部分,就是计算机的大脑---CPU.CPU也就是中央处理器(Central Processing Unit).中央处理器是一块超大规模的集成电路,是一台计算机的运算核心(C ...