思路:

模拟。

实现:

 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. oracle下 启动subversion命令 及 oracle相关服务启动备忘

    linux shell下  svnserve - d -r + 目录   例如:svnserve -d -r /svn 启动 svn服务. 访问svn://192.168.0.120/kjcg 测试. ...

  2. 多线程之:正确使用 Volatile 变量

    转载:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html Java™ 语言包含两种内在的同步机制:同步块(或方法)和 volatile ...

  3. [APIO 2010] 特别行动队

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1911 [算法] 设前i个士兵"修正"后的最大战斗力为fi 令su ...

  4. 如何更快更好的写出cnblog博客?windows live writer推荐

    之前总是会羡慕网上那些技术牛人的博客都写的那么给力,后来一搜发现还是有工具可用的. 这里就推荐一款写博客的"神器",Windows Live Writer (Get It Now! ...

  5. HTML head元素

    head标签中可以包含的标签元素有: <title>:定义html页面的标题 <meta>: <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解 ...

  6. 20个Flutter实例视频教程-第07节: 毛玻璃效果制作

    视频地址: https://www.bilibili.com/video/av39709290/?p=7 博客地址: https://jspang.com/post/flutterDemo.html# ...

  7. PHP中正则表达式学习及应用(四)

    正则表达式在PHP中的应用 1.匹配功能 2.替换功能 3.分割功能 例如: <?php $str="addsds{title}hfksjd{author}hfjdkjd{conn}j ...

  8. UVa 11520 Fill the Square (水题,暴力)

    题意:给n*n的格子里填上A-Z的字符,保证相邻字符不同,并且字典序最小. 析:直接从第一个格子开始暴力即可,每次判断上下左是不是相同即可. 代码如下: #pragma comment(linker, ...

  9. 51nod - 1420 - 数袋鼠好有趣 - 贪心 - 二分

    https://www.51nod.com/Challenge/Problem.html#!#problemId=1420 一开始乱搞了一发,每个袋鼠二分找最小的能放它的,然后二分的范围从下一个开始保 ...

  10. DB2 - 编目的解释

    编目(Catalog),是在本地或远程建立客户端到服务器的数据库连接的过程.其目的在于获取编目信息,即生成用来访问数据库的目录.系统数据库目录包含一个列表和指针,通过目录可以使 DB2 能够找到已知的 ...