7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only the add operator.

这道题让我们实现乘法加法和除法,而且规定了只能使用加法。那么我们先来看如何用加法来实现减法,我们知道,减去一个数就等于加上这个数的负数。那么我们先写一个求负数的函数,对于n来说,我们累计n个-1,对于-n来说,我们累计n个1。这样减法的就搞定了,我们再来看乘法,乘法也可以看做加法的一种,比如a乘以b,就是b个a相加的结果,我们最后来判断正负,如果a和b的符号相反,我们再调用之前的求负数的函数即可。最后就是除法了,除法也可以转为加法,比如a除以b,那么我们可以先初始化商为0,然后用商加上b和a比较,如果小的话,商就加一,再比较,以此类推直到大于等于a,符号也是最后来判断,这样我们就用加法实现了乘法减法和除法了,参见代码如下:

class Solution {
public:
int negate(int a) {
int res = , d = a < ? : -;
while (a != ) {
res += d;
a += d;
}
return res;
}
int minus(int a, int b) {
return a + negate(b);
}
int multiply(int a, int b) {
if (a < b) return multiply(b, a);
int res = ;
for (int i = ; i < abs(b); ++i) {
res += a;
}
return b > ? res : negate(res);
}
int divide(int a, int b) {
if (b == ) return INT_MAX;
int m = abs(a), n = abs(b);
int res = , product = ;
while (product + n <= m) {
product += n;
++res;
}
if ((a < && b < ) || (a > && b > )) return res;
else return negate(res);
}
};

LeetCode中有一道Divide Two Integers 两数相除的题,那道题的除法实现过程较为复杂,但是算法很高效,可以看看。

[CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法的更多相关文章

  1. [CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈

    3.1 Describe how you could use a single array to implement three stacks. 这道题让我们用一个数组来实现三个栈,书上给了两种方法, ...

  2. [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

    3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...

  3. [CareerCup] 8.1 Implement Blackjack 实现21点纸牌

    8.1 Design the data structures for a generic deck of cards. Explain how you would subclass the data ...

  4. [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表

    8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...

  5. Codeforces Round #653 (Div. 3) B. Multiply by 2, divide by 6 (数学)

    题意:有一个数\(n\),每次操作可以使\(n*=2\)或\(n/=6\)(如果能被整除),求最少操作次数使得\(n=1\),如果不满足,输出\(-1\). 题解:我们只要看\(n\)的质因子即可,如 ...

  6. leetcode:Multiply Strings(字符串的乘法)【面试算法题】

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  7. Divide Two Integers(模拟计算机除法)

    Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...

  8. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  9. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

随机推荐

  1. 使用OLE DB读写Excel

    说明: 使用这种技术的好处是无需引用对象,坏处是无法处理类似合并单元格这样的复杂情况 一些更新: 为了使用Office 2010,需要安装Microsoft Access 2010 数据库引擎可再发行 ...

  2. Asp.net Identity 2.0 作弊条

    Moving ASP.NET Identity model to class library http://stackoverflow.com/questions/23446919/moving-as ...

  3. 人机交互—对win10自带输入法的评价

    在我的电脑换成win10系统后我就用他自带的输入法,它可以中英文切换,用起来很方便,就用了它,就没有下载别的输入法. 用户界面:这个输入法的界面非常简单,没有像搜狗,百度之类的皮肤一说,看起来很简单, ...

  4. Effective Java 25 Prefer lists to arrays

    Difference Arrays Lists 1 Covariant Invariant 2 Reified at runtime Erased at run time 3 Runtime type ...

  5. 读书笔记——OpenGL超级宝典

    对于某些函数的理解 glClear和glClearColor glClearColor指定glClear清除特定缓冲区时使用的值. glFlush 让所有已发送的命令尽快的由实际的绘制引擎执行. gl ...

  6. SQLPLUS连接oracle

    SQLPlus 在连接时通常有三种方式 1. sqlplus / as sysdba 操作系统认证,不需要数据库服务器启动listener,也不需要数据库服务器处于可用状态.比如我们想要启动数据库就可 ...

  7. VMware 12Pro 安装MACOS 10.10

    前言 最近帮人MacBook PRO重新安装了下10.10,在加上用了IP6,对苹果系统很有好感,所以想自己装个mac系统玩一下.虽然有了surface pro3 但是看了时间久了厌了,好想买个MAC ...

  8. URAL 1430 Crime and Punishment

    Crime and Punishment Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  9. web前端的渐进增强式开发模型

    渐进增强是前端开发的根本基础.从根本的层面上讲,它可以将HTML,CSS,JavaScript这三者的功能分离开来,这能让当前的项目开一个好头.我们在创建项目的开始要将这三者分开,它们对应的称呼是结构 ...

  10. Auto Clear Unity Console Log

    功能 可以在Editor模式下执行,当然也可以Runtime模式下执行,自动清除 Console的log信息 功能需求 当在制作Editor的一些功能时,常常需要手动的点击Console窗口的Clea ...