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. LoadRunner脚本回放与设置

    一.runtime setting 1.迭代次数设置与迭代步长(循环间隔时间) 2.日志打印设置       二.实时观看回放 1.动态回放与静态回放(静态回放时,不会有逐行高亮显示:动态回放时高亮显 ...

  2. git ---查看工作状态和历史提交

    1.git查看状态 -git status 2.版权声明 版权声明:新建一个   LICENSE.txt   文件 开源协议:MIT   //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...

  3. 1.了解Objective-C语言

    了解Objective-C语言 ** Objective-C 语言是"消息结构"(messaging structure) 类似C++ .Java 是"函数调用" ...

  4. taskctl的后台字符界面登录不了解决办法

    今天在使用taskctl的designer时,十多分钟挂了2次,每次挂了之后就签不出来了,只能等半小时,然后在taskctl的QQ群里咨询了,给的解决方案是 http://www.taskctl.co ...

  5. 数组(Arry)几个常用方法的详解

    join() 方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. arrayObject.join(separator)separator 可选.指定要使用的分隔符.如果省略 ...

  6. 仿陌陌的ios客户端+服务端源码

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...

  7. java_String类练习

    public class StringTest { //1.模拟trim方法,去除字符串两端的空格 public static void main(String[] args) { String st ...

  8. opencv笔记

    加载图像: OpenCV支持图像格式Windows位图(bmp),便携式图像格式(pbm,pgm,ppm)和Sun光栅(sr,ras). Mat image = imread( imageName, ...

  9. C# defult关键字

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

  10. [LUOGU] P1113 杂物

    题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务完成是必要的,因为 ...