给定两个整数,分别表示分数的分子 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. HDU-1850-Being a Good Boy in Spring Festival-nim博弈

    一年在外 父母时刻牵挂春节回家 你能做几天好孩子吗寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场悄悄给爸爸买个小礼物主动地 强烈地 要求洗一次碗某一天早起 给爸妈用心地做回早餐 如果愿意 你还可以和爸妈 ...

  2. LightOJ-1282-Leading and Trailing-快速幂+数学

    You are given two integers: n and k, your task is to find the most significant three digits, and lea ...

  3. ps-奇幻金鱼彩妆

    1.打开背景图,拷贝一份防止出错 2增加色相饱和度 改变全局的饱和度.这是 为了改变嘴唇的颜色.其他变色的地方可以通过添加蒙版,然后用背景色为黑色的画笔擦掉 3给眼睛上加上金鱼  置入图片   类型选 ...

  4. 测试网中用户添加docker yum源

    /etc/yum.repo.d 中新建docker.repo 添加 [docker]name=CentOS-$releasever - Mediabaseurl=ftp://10.191.51.X/d ...

  5. Qt学习笔记----基础知识

    一.qt的本质 qt的本质是c++的图形界面类库,本身是mvc结构.qt能火最大程度 归功于它跨平台的特性,一次编码,多次编译应用. 注意:qt由于历史原因,经历了奇趣.诺基亚.digit公司,导致q ...

  6. Logstash2.3.4趟坑之集成Redis哨兵模式

    最新在使用Lostash2.3.4收集数据的时候,在读取redis数据的时候,报了如下的一个异常: 异常如下 Pipeline aborted due to error {:exception=> ...

  7. 在npm中使用bower包依赖工具

    什么是bower Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如YeoMan和 ...

  8. BBS论坛 自定义form组件

    二.自定义form组件 from django import forms from django.forms import widgets from app01 import models # 定制f ...

  9. 第一个Vus.js

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. UEditor 编辑模板

    读取模板,放到ueditor中进行编辑 @model WeiXin_Shop.Models.WX_GoodsDetails @Html.Partial("_MasterPage") ...