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(模拟计算机除法)的更多相关文章

  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. C++学习之继承篇

    今天通过对实验二继承,重载,覆盖的学习,让我更深一步理解了这些概念的区别. 首先来明确一个概念,函数名即地址,也就是说函数名就是个指针. 编译阶段,编译器为每个函数的代码分配一个地址空间并编译函数代码 ...

  2. bootstrap 整理

    form-control   输入框,独占一行,占满. form-group  增加下边距 <label> 字体会加粗,不是独占一行 .col-xs-  超小屏 .col-sm- 平板 . ...

  3. Mysql基本操作、C++Mysql简单应用、PythonMysql简单应用

    MySql基本操作 -- 当指定名称的数据库不存在时创建它并且指定使用的字符集和排序方式 CREATE DATABASE IF NOT EXISTS db_name CHARACTER SET UTF ...

  4. vue工程化:返回顶部和底部的动画效果

    . <template> <div> <div class="scroll" :class="{show:isActive}"&g ...

  5. python3+beautifulSoup4.6抓取某网站小说(一)爬虫初探

    本次学习重点: 1.使用urllib的request进行网页请求,获取当前url整版网页内容 2.对于多级抓取,先想好抓取思路,再动手 3.BeautifulSoup获取html网页中的指定内容 4. ...

  6. 笔试算法题(19):判断两条单向链表的公共节点 & 字符集删除函数

    出题:给定两个单向链表的头结点,判断其是否有公共节点并确定第一个公共节点的索引: 分析: 由于是单向链表,所以每个节点有且仅有一个后续节点,所以只可能是Y型交叉(每条链表中的某个节点同时指向一个公共节 ...

  7. linux下安装flash player

    下载flash player,选择.tar.gz for other linux. 网址:http://get.adobe.com/flashplayer/ Ctrl+Alt+T进入终端: 进入下载所 ...

  8. ubuntu 安装python 编程环境

    1. 安装python sudo add-apt-repository ppa:fkrull/deadsnakessudo apt-get updatesudo apt-get install pyt ...

  9. restframework框架之认证

    1. 认证之APIView 在聊APIView之前, 我们先重温一下django2.x的CBV流程 a. 对于django而言, 当浏览器请求到达之后,按照规则首先会经过各大中间件(Middlewar ...

  10. python 通过句柄获取窗口内容

    -- enoding:utf-8 -- 生成 buffer 对象 import win32con from win32gui import PyMakeBuffer, SendMessage, PyG ...