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. javascript 的默认对象

    一.日期对象 格式 :   日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用)                                      2.英文-参数格式 ...

  2. bootstrap3 兼容IE8浏览器

    近期在使用bootstrap这个优秀的前端框架,这个框架非常强大,框架里面有下拉菜单.按钮组.按钮下拉菜单.导航.导航条.面包屑.分页.排版.缩略图.警告对话框.进度条.媒体对象等,bootstrap ...

  3. c# 反射简单使用

    类库dll,将生成ExampleLib.dll文件 namespace ExampleLib { public class Example { public static string FuncA() ...

  4. 烂泥:KVM虚拟机的关机与开启

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 我们在开启与关闭KVM虚拟机时,一般是通过start.shutdown.reboot等命令来进行.但是有时候我们会发现在使用shutdown.reboo ...

  5. 10_放置街灯(Placing Lampposts,UVa 10859)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P70 例题30: 问题描述:有给你一个n个点m条边(m<n<=1000)的无向无环图,在尽量少的节点上放灯,使得所有边都被照 ...

  6. 虚拟机centos6.5 --设置主机名

    vi /etc/sysconfig/network #修改HOSTNAME后面的值,机器名 vi /etc/hosts #设置ip和机器名的对应关系 192.168.12.232 master 192 ...

  7. 【转载】4412开发板、PC、ubuntu通过网线连接

    今天看到使用TFTP方式,开发板直接从ubuntu下载程序,不需要一直通过烧写文件系统下载,我试了一下,虚拟机.开发板.pc三者老是互相ping不通.纠结了很久终于解决了. 写下这个小笔记,供大家参考 ...

  8. apache加载php配置

    #载入php模块和ini路径,以及凡是.php开头的以它来处理 LoadModule php5_module E:/server/php/php5apache2_2.dll PHPIniDir &qu ...

  9. excel vba 当cell的值变化时 进行判断操作

    示例效果: ----------- 在excel的sheet1中, 当A列的值 大于100时 ,其对应B列背景显示红色,C列显示"有数据" 否则,B列背景色正常,C列清空相应的数据 ...

  10. 边工作边刷题:70天一遍leetcode: day 84

    Flatten 2D Vector 要点: 这题是2d的iterator,一般对于1d的情况,hasNext()是不需要做移动的.而2d不同,core iterator是j向的,而i向要在hasNex ...