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. java集合 之 set 集合

    set集合可以存储多个对象,但并不会记住元素的存储顺序,也不允许集合中有重复元素(不同的set集合有不同的判断方法). 1.HashSet类 HashSet按照Hash算法存储集合中的元素,具有很好的 ...

  2. Hadoop Eclipse开发环境搭建

        This document is from my evernote, when I was still at baidu, I have a complete hadoop developme ...

  3. 在Eclipse中安装SVN客户端插件

    在Eclipse中安装SVN客户端插件 1.1  Eclipse插件应用市场 在Eclipse中访问Eclipse Marketplace Client可以搜索Subversion,下载插件,按提示安 ...

  4. javascript 特效实现(3)—— 鼠标滑过显示二级菜单效果

    1. 关键代码:使用 switch 或 if 判断语句,改变对应的二级菜单显示方式为 block 或 none function selectTabMenu(i){ switch(i){ case 7 ...

  5. 嵌入式Linux应用程序开发详解------(创建守护进程)

    嵌入式Linux应用程序开发详解 华清远见 本文只是阅读文摘. 创建一个守护进程的步骤: 1.创建一个子进程,然后退出父进程: 2.在子进程中使用创建新会话---setsid(): 3.改变当前工作目 ...

  6. Docker与容器快速入门

    Docker之风席卷全球,但很多人觉得docker入门确实不太容易,其原因在于很多知识点上没准备好,在docker解决了什么问题.怎么解决的.用什么技术解决的都还没想清楚的时候就去探索docker组件 ...

  7. uva 120 stacks of flapjacks ——yhx

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...

  8. 使用Burpsuite破解Webshell密码

    Burp Suite 是用于攻击web  应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP  消息,持久性,认证 ...

  9. 边工作边刷题:70天一遍leetcode: day 73

    Read N Characters Given Read4 I/II 要点:这题的要点就是搞清楚几个变量的内在逻辑:只有buffer是整4 bytes的.而client要读的bytes(需求)和实际上 ...

  10. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...