思路:

模拟。

实现:

 class Solution
{
public:
string fractionToDecimal(int numerator, int denominator)
{
long long a = numerator, b = denominator;
string ans = "";
if (a * b < ) ans += '-';
a = abs(a); b = abs(b);
ans += to_string(a / b);
a %= b;
if (!a) return ans;
ans += '.';
a *= ;
vector<long long> v;
map<pair<long long, long long>, int> mp;
long long rem = a % b, q = a / b;
while (rem && !mp.count(make_pair(rem, q)))
{
v.push_back(q);
mp[make_pair(rem, q)] = v.size() - ;
rem *= ;
q = rem / b;
rem %= b;
}
if (rem == )
{
v.push_back(q);
for (auto it: v) ans += to_string(it);
}
else
{
int start = mp[make_pair(rem, q)];
for (int i = ; i < start; i++) ans += to_string(v[i]);
ans += '(';
for (int i = start; i < v.size(); i++) ans += to_string(v[i]);
ans += ')';
}
return ans;
}
};

leetcode166 Fraction to Recurring Decimal的更多相关文章

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

    给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...

  2. 【leetcode】Fraction to Recurring Decimal

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

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

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

  4. 【LeetCode】166. Fraction to Recurring Decimal

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

  5. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

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

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

  7. 【刷题-LeetCode】166 Fraction to Recurring Decimal

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

  8. [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal

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

  9. LeetCode Fraction to Recurring Decimal

    原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...

随机推荐

  1. python 基础之第九天

    ###############错误和异常######################## 说明:e 是错误的具体原因,else 表示没有异常才会执行else的语句,finally 是无乱有没异常都要执 ...

  2. js跳转方式 【转】

    第一种:    <script language="javascript" type="text/javascript">           wi ...

  3. bzoj 4712 洪水 —— 动态DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4712 设 f[x] = min(∑f[u] , a[x]),ls = ∑f[lson] 矩阵 ...

  4. Linux 无法登陆172.***.***.***的子网

    1. sudo dhclient -r 这条命令重复执行几次 2. dhclient - 3.查看ifconfig

  5. kvm详细介绍

    KVM详解,太详细太深入了,经典 2016-07-18 19:56:38 分类: 虚拟化 原文地址:KVM详解,太详细太深入了,经典 作者:zzjlzx KVM 介绍(1):简介及安装 http:// ...

  6. Flutter实战视频-移动电商-55.购物车_底部结算栏UI制作

    55.购物车_底部结算栏UI制作 主要做下面结算这一栏目 cart_bottom.dart页面 先设置下内边距 拆分成三个子元素 全选 因为有一个文本框和一个全选的text文本,所以这里也用了Row布 ...

  7. 程序兵法:Java String 源码的排序算法(一)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 <程序兵法:Java Str ...

  8. lightoj 1125【背包·从n个选m个】

    题意: 给你 n 个背包,然后给你两个数,D,M,问你从n个里面挑M个出来,有多少种方法能够整除D: 思路: 试想我先不挑M个出来的话,仅仅是构造一个D的倍数,其实就是构造一个数的话, 其实就是个递推 ...

  9. CSS优先级、CSS选择器、编写CSS时的注意事项

    CSS的优先级: 内嵌样式>ID选择器>类选择器>标签选择器 内部样式>内部样式>外部样式 CSS的选择器: 选择器:在 CSS 中,选择器是一种模式,用于选择需要添加样 ...

  10. css文本之蛇

    文本之蛇 css把文本当做一行来处理,把他们放在一个看不见的盒子里面.盒子遇到容器的外边界会折行.所有的文本属性都应用于这个盒子,而不是包含文本的容器. 最有用的8个文本属性 文本缩进(text-in ...