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. Actionscript,AS3,MXML,Flex,Flex Builder,Flash Builder,Flash,AIR,Flash Player之关系

    转自zrong's blog:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的 ...

  2. 平衡图片负载,提升web站点访问体验

    最近给分公司做官方网站,内网测试一切ok,发布至云端后,体验惊人——公司外网网速渣渣(十几k~几十k),更加要命的是,网站的高清图,根本就加载不出来,几秒,十几秒过去了,仍然在转圈圈,如下图... 于 ...

  3. spring framework 第一章数据库管理(data access)

    spring data access 的网址:https://docs.spring.io/spring/docs/current/spring-framework-reference/index.h ...

  4. JData 整合ArtTemplate的前端框架

    因为项目需要和自己的兴趣,几个月前结合模板解析神速的ArtTemplate,自己写了个框架取名JData,多多指教啊---因为一直没时间写文档,为了能够更方便地使用和避免我把代码忘了,今天抽空把文档写 ...

  5. 【C++】模板简述(一):模板的引入

    我们在介绍模板之前,首先想象有这么一个场景: 我们需要通过C++写出一个通用的加法程序,那么有如下几种方法: 方法一:C++的函数重载 //int int int int Add(int l,int ...

  6. 洛谷 P3371 【模板】单源最短路径(堆优化dijkstra)

    题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...

  7. Bash Template

    #/bin/bash #set -x set -e usage() { cat <<EOF Usage: `basename $` [OPTIONS] <non-option arg ...

  8. (转)淘淘商城系列——分布式文件系统FastDFS

    http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...

  9. C#根據當前DataGridView查詢數據導出Excel

    private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.C ...

  10. vsphere中的vcenter创建esxi模板虚拟机新建无法连接网络

    1.删除网卡配置文件下的uuid和hwaddr  这是因为虚拟机模板创建网卡mac没改变 2.删除规则文件 rm -f /etc/udev/rules.d/-prtsistent-net.rules ...