引言

数学计算的模拟类题目,往往是要求实现某种计算(比如两数相除),实现的过程中会有所限定,比如不允许乘法等等。

这类题目首先要注意计算过程中本身的特殊情况。比如求相除,则必须首先反映过来除数不能为0。

其次要记得考虑负数的情况,如果计算范围不单单是整数,还要考虑double的比较方式。

最后要注意越界情况,这个是最容易犯错的,只能具体问题具体分析。

例题 1 不用"+ - * / "做加法

这道题来自于剑指Offer,为了归类,我把它放到了这里。

面试题 47(*),不用加减乘除做加法(位运算易想到,怎么用就是个技术活了)(附 不定义新变量前提下交换两数的方法)

例题 2 Pow(x, n)

Implement pow(xn).

class Solution {
public:
double pow(double x, int n) { }
};

这道题目我在 面试题 11,求double类型的n次幂(double数值的比较不能用==,求幂的logN复杂度方法) 中写过。

要注意的就是:

1) 0 的 0 次幂无意义。

2) 0的负数次幂无意义。

3) 所有非零数的 0次幂为1。

4) double x 判断是否为0时,不能用简单的 == 0判断,因为double类型总是存在误差。

实现,时间复杂度 log(n),"n"是函数中的参数n

class Solution {
public:
double pow(double x, int n) {
if(n == ) return ;
if(equal(x, 0.0)) return ;
return (n > ? powCore(x, n) : 1.0/powCore(x, -n));
}
private:
bool equal(double x, double y){
if((x-y) < 0.00001 && (x-y) > -0.00001) return true;
return false;
}
double powCore(double x, int n){
if(n == ) return ;
double tmp = pow(x, n/);
return tmp*tmp*(n& ? x : );
}
};

另一个不用递归直接迭代实现的版本:

class Solution {
public:
double pow(double x, int n) {
if(n == ) return ;
if(n < && (x < 0.000001 && x > -0.000001)){
if(n < ) return ( << );
}
bool pos = (n > );
unsigned int tmp = abs(n);
double res = 1.0;
while(tmp){
if(tmp&) res *= x;
tmp = tmp >> ;
x *= x;
}
return (pos ? res : (double)(1.0/res));
}
};

例题 3 Divide Two Integers

Divide two integers without using multiplication, division and mod operator.

class Solution {
public:
int divide(int dividend, int divisor) {
}
};

这道题相较于前一道稍复杂些。首先考虑divisor为0的情况,再考虑负数的情况。

接着考虑解体方法,由于乘除都不能用,只能用加法,而如果直接累加自然会超时。我的思路是定义一个长32的数组path[32],path[0] = divisor, path[i] = path[i-1] + path[i-1]。path[32]不一定全被填满,当计算出path[i] > dividend时,path[i] 就不会被记录。由于path[i] 有大于 dividend的可能,因此临时存储计算结果的数定义为long long。

然后用这个path[32] 去凑成dividend,相除结果其实就是凑得过程中 1 << i 的相加(i 是 path[] 的 index)。

第一版代码如下:

class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor == ) return ;
if(dividend == ) return ; bool minus1 = false, minus2 = false, minus = false;
if(divisor < ){
divisor = ( - divisor);
minus1 = true;
}
if(dividend < ){
dividend = ( - dividend);
minus2 = true;
}
minus = (minus1 ^ minus2); //结果的正负号, minus若为true,结果就添加负号。 if(dividend < divisor) return ;
long long cache = divisor;
int* path = new int[];
int ind = ;
for(; cache <= dividend; path[ind] = (int)cache, ++ind, cache += cache); //填充path[]
cache = path[--ind]; int res = << ind; //从path的最末尾开始凑dividend
while(ind >= ){
if(cache == dividend) return minus ? (-res) : res;
if(cache > dividend){
cache -= path[ind];
res -= << ind;
}
for(--ind; ind >= && cache < dividend; cache += path[ind], res += << ind);
}
return minus ? (-res) : res;
}
};

这版代码在执行 sln.divide(-1010369383, -2147483648) 出现错误。

原因在于 开始的

dividend = (0 - dividend);

补码表示下,int下限绝对值比上限绝对值大1。dividend = -2147483648时,0-dividend 结果并非是2147483648。因此dividend 和 divisor的绝对值应该用unsigned int 表示。

考虑到这一点,当dividend = -2147483648 时,res 和 path 也有越过 int上限的可能,因此它们应该定义为 unisgned int。

改进后的代码,改动了一些参数的 格式,这次AC了。

class Solution {
public:
int divide(int dividend, int divisor) {
if(divisor == ) return ;
if(dividend == ) return ; bool minus1 = false, minus2 = false, minus = false;
unsigned int divd, divr;
if(divisor < ){
divr = ( - divisor);
minus1 = true;
}else{
divr = divisor;
}
if(dividend < ){
divd = ( - dividend);
minus2 = true;
}else{
divd = dividend;
}
minus = (minus1 ^ minus2); //结果的正负号, minus若为true,结果就添加负号。 if(divd < divr) return ;
long long cache = divr;
unsigned int* path = new unsigned int[];
int ind = ;
for(; cache <= divd; path[ind] = (unsigned int)cache, ++ind, cache += cache); //填充path[]
cache = path[--ind]; unsigned int res = << ind; //从path的最末尾开始凑dividend
while(ind >= ){
if(cache == divd) return minus ? (-res) : res;
if(cache > divd){
cache -= path[ind];
res -= << ind;
}
for(--ind; ind >= && cache < divd; cache += path[ind], res += << ind);
}
return minus ? (-res) : res;
}
};

[LeetCode] 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers的更多相关文章

  1. leetcode 338. Counting Bits,剑指offer二进制中1的个数

    leetcode是求当前所有数的二进制中1的个数,剑指offer上是求某一个数二进制中1的个数 https://www.cnblogs.com/grandyang/p/5294255.html 第三种 ...

  2. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  3. LeetCode:“剑指 Offer”

    LeetCode:"剑指 Offer" 刷题小菜鸡,花了几天时间做了一遍 LeetCode 上给出的 "剑指 Offer" 在此做一下记录 LeetCode主页 ...

  4. 剑指offer编程题Java实现——面试题12相关题大数的加法、减法、乘法问题的实现

    用字符串或者数组表示大数是一种很简单有效的表示方式.在打印1到最大的n为数的问题上采用的是使用数组表示大数的方式.在相关题实现任意两个整数的加法.减法.乘法的实现中,采用字符串对大数进行表示,不过在具 ...

  5. [leetcode] 剑指 Offer 专题(一)

    又开了一个笔记专题的坑,未来一两周希望能把<剑指Offer>的题目刷完

  6. 【剑指Offer】不用加减乘除做加法 解题报告(Java)

    [剑指Offer]不用加减乘除做加法 解题报告(Java) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题 ...

  7. 剑指Offer——网易校招内推笔试题+模拟题知识点总结

    剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...

  8. 【Java】 剑指offer(65) 不用加减乘除做加法

      本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.×. ...

  9. 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)

    剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...

随机推荐

  1. AJAX学习2

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正. 本文学习内容:https://www. ...

  2. 基于spec评论作品 - 探路者 贪吃蛇

    基于spec评论作品,试用(并截图)所有其他小组的Alpha作品,与软件功能说明书对比,评论Alpha作品对软件功能说明书的实现. 首先通过命令行进入到游戏主页面中. 因为软件没有编译为exe程序,所 ...

  3. 20162328蔡文琛 week06

    20162328 2017-2018-1 <程序设计与数据结构>第6周学习总结 教材学习内容总结 队列元素按FIFO的方式处理----最先进入的元素最先离开. 队列是保存重复编码k值得一种 ...

  4. 复利计算1.0,2.0,3.0(java)

    程序源代码: import java.util.Scanner; public class ch { public static void main(String[] args) { Scanner ...

  5. 使用vue的mixins混入实现对正在编辑的页面离开时提示

    mixins.ts import { Vue, Component, Watch } from "vue-property-decorator" Component.registe ...

  6. 编写shell时,遇到let: not found错误及解决办法

    #!/bin/bashi=1sum=0while [ $i -le 100 ]do  let sum=sum+$i  let i++ done 在写一个简单的循环脚本时,报错 let: not fou ...

  7. 【设计模式】C++中的单例模式

    讲解来自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4281275&uid=26611383 由于使用了POSIX函 ...

  8. @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值

    @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值

  9. 【uoj#310】[UNR #2]黎明前的巧克力 FWT

    题目描述 给出 $n$ 个数,从中选出两个互不相交的集合,使得第一个集合与第二个集合内的数的异或和相等.求总方案数. 输入 第一行一个正整数 $n$ ,表示巧克力的个数.第二行 $n$ 个整数 $a_ ...

  10. android面试(1)----布局

    1.说出android 五中布局,并说出各自作用? FrameLayout: 堆叠布局,也是就可以堆在一起.最长应用于Fragment的使用上. LinearLayout: 线性布局,可以是竖排或水平 ...