Divide two integers without using multiplication, division and mod operator.

由于不能用乘号,除号,和取余。那么一个数除另外一个数,如a/b,实际含义是a中有多少个b,我们可以多次计算a-b,并更新a,最后a-b<0说明循环结束,循环的次数也即结果。

但是上面这种方法超时,如2147483647/1,那么循环次数为2147483647次。

所以考虑二分法来减少循环的次数。而且乘2,相对于左移一位,没有用到乘号。

代码:

  1. class Solution {
  2. private:
  3. int res;
  4. public:
  5. int solve(long long dividend, long long divisor){
  6. long long temp=;
  7. while (divisor<=dividend)
  8. {
  9. divisor=divisor<<;
  10. temp=temp<<;
  11. }
  12. divisor=divisor>>;
  13. temp=temp>>;
  14. res+=temp;
  15.  
  16. return dividend-divisor;
  17.  
  18. }
  19. int divide(int dividend, int divisor) {
  20. res=;
  21. bool fu=false;
  22. if(divisor==) return -;
  23. if(divisor==) return dividend;
  24.  
  25. long long my_dividend=(long long)dividend;
  26. long long my_divisor=(long long)divisor;
  27. if(my_dividend<&&my_divisor<){
  28. my_dividend=-my_dividend;
  29. my_divisor=-my_divisor;
  30. fu=false;
  31. }
  32. if(my_dividend<) {my_dividend=-my_dividend;fu=true;}
  33. if(my_divisor<) {my_divisor=-my_divisor;fu=true;}
  34.  
  35. while (((my_dividend=solve(my_dividend,my_divisor))-my_divisor)>=);
  36.  
  37. if(fu) res=-res;
  38. return res;
  39. }
  40. };
  41. int main()
  42. {
  43. freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
  44. Solution so;
  45. int a=-;
  46. int b=-;
  47. cout<<so.divide(a,b)<<endl;
  48. return ;
  49. }

Divide Two Integers(模拟计算机除法)的更多相关文章

  1. [leetcode]29. Divide Two Integers不用除法实现除法

    思路是不断将被除数分为两部分,每次分的一部分都是尽量大的除数的倍数,然后最后的商就是倍数加上剩下的部分再分,知道不够大. 递归实现 剩下的难点就是,正负号(判断商正负后将两个数都取绝对值),数太大(将 ...

  2. [LeetCode] Divide Two Integers 两数相除

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  3. leetcode-【中等题】Divide Two Integers

    题目 Divide two integers without using multiplication, division and mod operator. If it is overflow, r ...

  4. LeetCode29 Divide Two Integers

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  5. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  6. [Math]Divide Two Integers

    otal Accepted: 54356 Total Submissions: 357733 Difficulty: Medium Divide two integers without using ...

  7. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  8. leetcode第28题--Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. 分析:题目意思很容易理解,就是不用乘除法和模运 ...

  9. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

随机推荐

  1. 7.JAVA-类继承、覆写、final关键字

    1.JAVA继承-extends 在java中,要想实现继承则使用extends关键字. 一般子类被称为派生类,父类称为基类(super) extends需要注意的地方: java不允许多重继承(一个 ...

  2. 将call/apply方法应用于其他对象上的几种方法

    在处理类数组中,发现了两种将数组方法应用于类数组的方法,现将call/apply的常用方式总结一下. 一.当做函数调用 function print_vars(var1,var2){ console. ...

  3. flex弹性布局操练1

    实现弹性布局之前常用浮动,相对定位和绝对定位等,但是现在好了,随着flex的兴起,方便了很多,而且也符合未来响应式布局的方向. 理论的东西可参考css3手册,这里专注实操. 一:单个元素 <di ...

  4. Scala基础篇-01变量,数据类型

    一.Scala变量 共有3种变量修饰符: val: 可变 var: 不可变 lazy val: 第一次应用时才计算 二.Scala的数据类型 1)类型不匹配:高精度数值赋给低精度数据类型. 2)Uni ...

  5. Winform webbrowser 隐藏 html 元素

    目的:用webbrowser打开网页,并隐藏网页上某个html元素 1.如果已知元素ID,比较好办 直接使用webbrowser1.Document.getElementById("id&q ...

  6. windows ubuntu bcdeditor

    双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...

  7. js 删除数组中某一项的几种方法总结

    第一种:改变原数组 借用原生数组方法:splice(index,len,[item])  剪接 借用原生对象方法:delete array[index] + array.slice(0, index) ...

  8. caffe数据读取

    caffe的数据读取分为lmdb和 待清理,包括fast 这个一系列是怎么转换成lmdb数据的

  9. C# defult关键字

    一.问题 今天写一个函数提示用defult,因为第一次用记录一下 public static T GetConfig<T>(string strConfig) { try { return ...

  10. 计算机中的CPU

    今天写一下计算机中最核心的一部分,就是计算机的大脑---CPU.CPU也就是中央处理器(Central Processing Unit).中央处理器是一块超大规模的集成电路,是一台计算机的运算核心(C ...