给定两个整数,分别表示分数的分子 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二次开发-更改图纸尺寸内容(编辑文本)uc5566

    #include <uf.h> #include <uf_drf.h> #include <uf_obj.h> #include <uf_part.h> ...

  2. python入门 类的继承和聚合(五)

    继承 class Rocket: def __init__(self, name, distance): self.name = name self.distance = distance def l ...

  3. python 测试框架nose

    python测试框架nose nose不是python自带模块,这里我才用pip的方式安装 pip install nose 这样就完成了安装,然后再确认下是否安装成功了,直接打开cmd输入noset ...

  4. PAT_A1090#Highest Price in Supply Chain

    Source: PAT A1090 Highest Price in Supply Chain (25 分) Description: A supply chain is a network of r ...

  5. UVA 11178 /// 向量旋转 两向量夹角

    题目大意: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  6. spring @Transactional注解参数详解(13)

    事物注解方式: @Transactional 当标于类前时, 标示类中所有方法都进行事物处理 , 例子: 1 @Transactional public class TestServiceBean i ...

  7. Nginx常用功能配置一

    Nginx常用功能配置 参数include配置 说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目 ...

  8. sparkStreaming的transformation和action详解

    根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transformations Window Operations J ...

  9. java字符串简单介绍

    String:String对象初始化之后不可变线程安全简单的字符串操作使用String效率更高 StringBuffer:StringBuffer对象初始化之后可改变线程安全频繁的字符串操作可以使用S ...

  10. COGS2356 【HZOI2015】有标号的DAG计数 IV

    题面 题目描述 给定一正整数n,对n个点有标号的有向无环图进行计数. 这里加一个限制:此图必须是弱连通图. 输出答案mod 998244353的结果 输入格式 一个正整数n. 输出格式 一个数,表示答 ...