给定两个整数,分别表示分数的分子 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. ARC032 D - アットコーダーモンスターズ

    https://arc032.contest.atcoder.jp/tasks/arc032_4# 切比雪夫距离,放在3000*3000的平面上, 一个集合就是恰好包含这个集合的矩形,价值是矩形长.宽 ...

  2. csp-s模拟测试97

    csp-s模拟测试97 猿型毕露.水题一眼秒,火题切不动,还是太菜了. $T1$看了一会儿感觉$woc$期望题$T1??$假的吧??. $T2$秒. $T3$什么玩意儿. 40 01:24:46 00 ...

  3. java: java中的 getInstance() 的理解

    原文地址:https://blog.csdn.net/qq_26293573/article/details/78184844 在单例模式下使用 . 单例模式:所谓单例模式就是一个类有且只有一个实例, ...

  4. Codeforces 1163A - Eating Soup

    题目链接:http://codeforces.com/problemset/problem/1163/A 题意:n 只猫围成一圈,离开 m 只,最多剩下几组猫. 思路:当 n == m 即猫都离开时 ...

  5. selenium基础-跳过验证码

    selenium基础-跳过验证码 一.方法 设置万能验证码或者屏蔽验证码(最常用的方法) 使用验证码识别工具识别验证码 通过selenium操作cookies 直接使用配置文件的webdriver 二 ...

  6. Hadoop的局限与不足

  7. PHP出现报警后需要修改 date.timezone 的值(php.ini)

    PHP调试的时候出现了警告: It is not safe to rely on the system解决方法,其实就是时区设置不正确造成的,本文提供了3种方法来解决这个问题. 实际上,从PHP 5. ...

  8. 用于扩展目标跟踪的笛卡尔B-Spline车辆模型

    (哥廷根大学) 摘要 文章提出了一种表示空间扩展物体轮廓的新方法,该方法适用于采用激光雷达跟踪未知尺寸和方向的车辆.我们在笛卡尔坐标系中使用二次均匀周期的B-Splines直接表示目标的星 - 凸形状 ...

  9. vue SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

    在使用vue_cli时出现如下错误: 原因是  node  版本太低 应该升级

  10. phonegap 开发指南系列(3) ----在Eclipse中Android开发环境搭建

      前提条件:已在Eclipse中安装好Android SDK 和 ADT. 1.下载PhoneGap,解压. 2.用Eclipse新建一个安卓项目. 3.将phoneGap解压包里的Android文 ...