给定两个整数,分别表示分数的分子 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. NX二次开发-UFUN遍历图层UF_LAYER_cycle_by_layer

    NX11+VS2013 #include <uf.h> #include <uf_layer.h> #include <uf_ui.h> UF_initialize ...

  2. NX二次开发-获取按钮的ID UF_MB_ask_button_id

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...

  3. I/O与NIO(异步I/O)

    1.原来的I/O库与NIO最重要的区别是数据打包和传输方式的不同,原来的I/O以流的方式处理数据,而NIO以块的方式处理数据. 面向流的I/O系统一次一个字节地处理数据.一个输入流产生一个字节的数据, ...

  4. ionic:目录

    ylbtech-ionic:目录 1.返回顶部 1. http://www.runoob.com/ionic/ionic-tutorial.html 2. 2.返回顶部   3.返回顶部   4.返回 ...

  5. 得益于AI,这五个行业岗位需求将呈现显著增长趋势

    得益于AI,这五个行业岗位需求将呈现显著增长趋势 人工智能与人类工作是当下许多人津津乐道的一个话题,而讨论的重点大多是围绕在"未来人工智能会不会抢走我们的工作"这个方面.本文作者 ...

  6. UVA 12676 Inverting Huffman

    题目链接:https://vjudge.net/problem/UVA-12676 题目大意 一串文本中包含 N 个不同字母,经过哈夫曼编码后,得到这 N 个字母的相应编码长度,求文本的最短可能长度. ...

  7. nodejs中命令行和node交互模式的区分

    来自:廖雪峰教程 么么哒~ 命令行模式和Node交互模式 请注意区分命令行模式和Node交互模式. 看到类似C:\>是在Windows提供的命令行模式: 在命令行模式下,可以执行node进入No ...

  8. 搞懂这7个Maven问题,带你吊打面试官!

    Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:张丰哲 www.jianshu.com/p/20b39ab6a88c 在如今的互联网项目开发当中,特别是Java领域, ...

  9. Spring AOP源码分析(二):AOP的三种配置方式与内部解析实现

    AOP配置 在应用代码中,可以通过在spring的XML配置文件applicationContext.xml或者基于注解方式来配置AOP.AOP配置的核心元素为:pointcut,advisor,as ...

  10. 《转》python8元组

    转自 http://www.cnblogs.com/BeginMan/p/3156235.html 一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这 ...