原题地址

计算循环小数

先把负数转化成正数,然后计算,最后添加符号

当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数

需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧,比Hard难度的题还低。

最残暴的做法是直接转成64位长整型,比如下面的代码。好处是代码简洁了许多,不过这是投机取巧,因为如果题目把参数换成两个64为长整型,这个方法就不行了。

如果不用64长整型,就用32位普通整型怎么办?

由于我们要把除数和被除数转化成正数,所以当其中任意一个数等于INT_MIN时,就得考虑溢出问题。

对于被除数,情况还好。假如被除数是INT_MIN,大不了我先减一个除数,让商加1就可以避免这个问题。这个技术可以用在Divide Two Integers这道题中(参见这篇文章

对于除数,情况就复杂了。假如除数是INT_MIN,除数不能拆分,所以没法转换成正数去做。这怎么办?

有人可能会想,用无符号类型呗。的确,32位无符号数的最大表示范围是2^32 - 1 > 2^31,可以将除数转化成正数了。但是仍然会有溢出发生,因为在计算小数部分时,被除数如果小于除数就要不断乘以10,直到够除为止。这个不断乘以10的过程会溢出,因为无符号整型最多只能保存2倍大的除数(INT_MIN),而被除数很容易超过这个值。为了解决这个问题,需要将除法运算退化成减法去做,将乘法运算退化成加法。把乘以10改成加9次,把除以除数改成减若干次除数。显然,这样做太麻烦了。

也有人可能会想,干嘛要转成正数呢?直接在int上做呗,对不起,还会遇到上面所说的溢出的情况。

看来只使用32位数据类型没有办法了,如果非要坚持不用64位整型,也可以,大整数除法等待着你。

代码:

 string fractionToDecimal(int numerator, int denominator) {
if (numerator == )
return ""; long long num = numerator;
long long den = denominator;
map<long long, int> record;
string res = (numerator ^ denominator) < 0 ? "-" : "";
string rem;
int i = ; num = abs(num);
den = abs(den);
res += to_string(num / den);
num = (num % den) * ; while (num) {
if (record.find(num) == record.end())
record[num] = i;
else {
rem = rem.substr(, record[num]) + "(" + rem.substr(record[num]) + ")";
break;
}
rem += to_string(num / den);
num = (num % den) * ;
i++;
}
if (!rem.empty())
res += "." + rem; return res;
}

Leetcode#166 Fraction to Recurring Decimal的更多相关文章

  1. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  2. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  3. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  4. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  5. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  6. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  7. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  8. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

随机推荐

  1. ASP.NET的错误处理机制之二(实例log4net)

    一.log4net下载:http://logging.apache.org/log4net/download_log4net.cgi 二.web.config配置如下: <?xml versio ...

  2. js闭包理解实例小结

    Js闭包 闭包前要了解的知识  1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...

  3. mysql 排重查询

    GROUP BY 语句可以实现某一列的去重查询. 直接上语句: select io_dev_id from io_info where (TID=1 AND host_name='yang1') GR ...

  4. MYSQL查询某字段中以逗号分隔的字符串的方法

    首先我们建立一张带有逗号分隔的字符串. CREATE TABLE test(id int(6) NOT NULL AUTO_INCREMENT,PRIMARY KEY (id),pname VARCH ...

  5. MySQL性能优化笔记整理

    一.测试篇 1.测试目的,就是量化找出短板(基础参数配置) 2.测试三大指标 IOPS:每秒处理的IO请求数,即IO响应速度(注意和IO吞吐量的区别) QPS:每秒请求(查询)次数 TPS:每秒事务数 ...

  6. Box of Bricks最小移动砖块数目

    Description Little Bob likes playing with his box of bricks. He puts the bricks one upon another and ...

  7. C++类实现三维数组算法

    在学习北京大学教授的<程序设计实习 / Practice on Programming>中,遇到了一个习题,花了很长时间研究,现在分享出来: 课题地址:https://class.cour ...

  8. ORA-08189

    OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...

  9. 九度oj 1521 二叉树的镜像

    原题链接:http://ac.jobdu.com/problem.php?pid=1521 水题,如下.. #include<algorithm> #include<iostream ...

  10. 【linux命令系列】熟练运用每一个光标移动到最前和最后

    ctrl+e?a和e      ahead 和 end 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下 ...