[CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法
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 实现乘法减法和除法的更多相关文章
- [CareerCup] 3.1 Implement Three Stacks using Array 使用数组来实现三个栈
3.1 Describe how you could use a single array to implement three stacks. 这道题让我们用一个数组来实现三个栈,书上给了两种方法, ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- [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 ...
- [CareerCup] 8.10 Implement a Hash Table 实现一个哈希表
8.10 Design and implement a hash table which uses chaining (linked lists) to handle collisions. 这道题让 ...
- Codeforces Round #653 (Div. 3) B. Multiply by 2, divide by 6 (数学)
题意:有一个数\(n\),每次操作可以使\(n*=2\)或\(n/=6\)(如果能被整除),求最少操作次数使得\(n=1\),如果不满足,输出\(-1\). 题解:我们只要看\(n\)的质因子即可,如 ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Divide Two Integers(模拟计算机除法)
Divide two integers without using multiplication, division and mod operator. 由于不能用乘号,除号,和取余.那么一个数除另外 ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- H5文件操作API
引言 在之前我们操作本地文件都是使用flash.silverlight或者第三方的activeX插件等技术,由于使用了这些技术后就很难进行跨平台.或者跨浏览器.跨设备等情况下实现统一的表现,从另外一个 ...
- virtualbox 安装 虚拟机的时候报错不能创建新任务
找到原因是因为自己的windows是破解的, 找到C:\Windows\system32\uxtheme.dll这个文件,我的破解的windows在这里自带了一个uxtheme.dll.backup的 ...
- JavaScript Patterns 4.3 Returning Functions
Use closure to store some private data, which is accessible by the returned function but not to the ...
- 问题解决——OpenGL超级宝典 关于gltDrawTorus的错误解决
看OpenGL超级宝典的时候,遇到一个函数 “gltDrawTorus”,在TRANSFORM和SPHEREWORLD中都有用到.但是一开始在自己写示例代码里时却没法使用,而在作者的代码目录结构下却可 ...
- 利用Windows自带的Certutil查看文件MD5
当遇到需要对比两个文件是否一致时,可以使用下面的命令来显示文件的MD5, 然后对比两个文件的MD5码. certutil -hashfile <filename> MD5 命令的相关帮助信 ...
- java自带命令工具
jstat,这个工具很强大,可以监测Java虚拟机GC多方面的状态,具体参数含义参见此链接: ./jstat -gc 84012 1000 3 S0C S1C S0U S1U ...
- c++字符串互相转换
1.string vs char* //string to char* string str; const char* cch = str.c_str(); ]; strcpy(ch,cch); // ...
- 关于TouchEvent里面的touches,targetTouches,changedTouches的解释
touches:手指触摸到屏幕上引起的当前所有触摸点的集合; targetTouches:手指触摸到绑定事件的节点上的触摸点的集合; changedTouches:触摸事件时改变触摸点的集合; 以下 ...
- [转]移动web开发中meta标签作用
今天在尝试做移动页面的时候遇到了一个问题,<meta content="telephone=no,email=no" name="format-detection& ...
- 【Javascript Demo】JS获取当前对象大小以及屏幕分辨率等
效果如下: 代码如下: <html> <head> <title>获取当前对象大小以及屏幕分辨率等</title> <body> <d ...