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. iOS Programming Dynamic Type 2

    iOS Programming Dynamic Type  2       You will need to update two parts of this view controller for ...

  2. crontab安装及使用

    linux下crontab安装yum -y install crontabs service crond start     //启动服务service crond stop      //关闭服务s ...

  3. Spring Boot(15)——自动配置Validation

    自动配置Validation当应用中的Classpath下存在javax.validation的实现时,Spring Boot的org.springframework.boot.autoconfigu ...

  4. Log4j 配置某个类中某个方法的输出日志到指定文件

    我们在项目中使用log4j开发的时候,会遇到一些特殊的情况,比如:要输出某个类中某个方法的日志信息到文件中,方便以后查看 可以使用如下配置: log4j.rootLogger=info,stdout ...

  5. SqlServer2012学习 - 基本数据类型认知

    精确数字: 1.整数 int是Sql Server主要整数类型.tinyint,smallint,int 不会自动转成bigint. 大于 2,147,483,647 的整数常量将转换为 decima ...

  6. CAD使用SetxDataString写数据(com接口)

    主要用到函数说明: MxDrawEntity::SetxDataString 写一个字符串扩展数据,详细说明如下: 参数 说明 [in] BSTR val 字符串值 szAppName 扩展数据名称 ...

  7. 使用webpack+vue.js构建前端工程化

    参考文章:https://blog.csdn.net/qq_40208605/article/details/80661572 使用webpack+vue.js构建前端工程化本篇主要介绍三块知识点: ...

  8. 【计算机网络】2.3 文件传输协议:FTP

    第二章第三节 文件传输协议:FTP 在一个典型的FTP(File Transfer Protocol,文件传输协议)会话中,用户坐在一台主机(本地主机)前面,向一台远程主机传输(或接收来自远程主机的) ...

  9. MSYS2 使用

    在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...

  10. 将java project打包成jar包,web project 打包成war包的几种演示 此博文包含图片

    转: http://blog.csdn.net/christine_ruan/article/details/7491559 http://developer.51cto.com/art/200907 ...