给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数。

如果小数部分为循环小数,则将循环的部分括在括号内。

示例 1:

输入: numerator = 1, denominator = 2 输出: "0.5"

示例 2:

输入: numerator = 2, denominator = 1 输出: "2"

示例 3:

输入: numerator = 2, denominator = 3 输出: "0.(6)"

考验除法的原理。

还有很多需要注意的点。

  class Solution {
public:
string fractionToDecimal(int numerator, int denominator)
{
//特殊情况的判断。
if (denominator == 0)
return "NaN";
if (numerator == 0)
return "0";
//为什么要在里面在加一层long long,因为如果是INT_MIN那么在转换成long long前,就直接爆了。
//INT_MIN = -2147483647 - 1 而INT_MAX = 2147483647;
//该题如果不换成longlong,那么在临界值旁会出错。
long long n = abs((long long)numerator);
long long d = abs((long long)denominator);
//
bool flag = false;
if (numerator > 0 && denominator > 0 || numerator < 0 && denominator < 0)
{
flag = true;
}
string intergetPart = to_string(n / d);
n %= d;
vector<long long> save;
string decimalStr = "";
while (n != 0)
{
n *= 10;
int i = save.size() - 1;
for (; i >= 0; i -- )
{
//除法的原理就是除不尽的,乘10再除,最后将结果再缩小10倍
//如果之前的n有个和现在的n相同的数,那么就肯定是循环了。
if (save[i] == n)
break;
}
if (i >= 0)
{
decimalStr.insert(i, "(");
decimalStr += ")";
break;
}
save.push_back(n);
decimalStr += to_string(n / d);
n %= d;
}
//还需要注意答案为正还是负
return (flag == false ? "-" : "") + intergetPart + (decimalStr == "" ? "" : ("." + decimalStr));
}
};

Leetcode166. Fraction to Recurring Decimal分数到小数的更多相关文章

  1. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

  2. [LeetCode] Fraction to Recurring Decimal 分数转循环小数

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

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

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

  4. [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数

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

  5. leetcode166 Fraction to Recurring Decimal

    思路: 模拟. 实现: class Solution { public: string fractionToDecimal(int numerator, int denominator) { long ...

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

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

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

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

  8. 【leetcode】Fraction to Recurring Decimal

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

  9. 【LeetCode】166. Fraction to Recurring Decimal

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

随机推荐

  1. bitset简单用法

    bitset的创建: #include<bitset> bitset<32> ar; //默认全为0 bitset<32> ar(n); //n的二进制 bitse ...

  2. mac 安装并使用 mysql 或者 mac mysql 忘记密码,Can't connect to local MySQL server through socket homebrew

    1. brew install mysql 2. 启动mysql mysql.server start 我遇到了这个error,查openstack解决,我在这粘一下 ### Error:Can't ...

  3. CodeForces 1166E The LCMs Must be Large

    题目链接:http://codeforces.com/problemset/problem/1166/E 说明 LCM(一个集合) 为这个集合中所有元素的最小公倍数. 如果$A \subseteq B ...

  4. 20140329 自由 youtube

    1.人生入戏,全靠演技:人生苦短,必须性感 2.youtube修改用户名 3.使用代理软件修改了IE的代理导致上不了网

  5. LightOJ 1245 - Harmonic Number (II)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:仿照上面那题他想求这么个公式的数.但是递归太慢啦.让你找公式咯. ...

  6. 无LoadLibrary获取指定模块基址

    实际上,这块可以写成汇编,然后做远程注入用 方法 1.通过fs:[30h]获取当前进程的_PEB结构 2.通过_PEB的Ldr成员获取_PEB_LDR_DATA结构 3.通过_PEB_LDR_DATA ...

  7. 21个CSS技巧

    级联样式表(CSS)在当代Web设计中已经成为重要的环节,如果没有CSS现在的网站将像10年前一样不堪入目.随着CSS技术的普及,越来越多的高质量CSS教程涌入互联网,让我们的学习更加方便. 1.CS ...

  8. webpakc4.0移除了 CommonsChunkPlugin 组建

    在4.0之前我们可以通过 require('webpack.optimize.CommonsChunkPlugin');  这样引入 CommonsChunkPlugin  组建对第三方模块进行独立打 ...

  9. pd.Panel转化成json,然后再还原回来

    在使用tornado的write时候有一个需求,是将panel转化成json;而接收端再将json还原成panel格式. 尝试了很久,终于实现了. panel1 =pd.Panel({"on ...

  10. vue better-scroll 下拉上拉,加载刷新

    _initScroll(){             this.$nextTick(() => {                 if (!this.scroll) {             ...