思路:

模拟。

实现:

 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. Lucas定理和扩展Lucas定理

    1.Lucas定理 首先给出式子:\(C_n^m\%p = C_{\lfloor\frac{n}{p}\rfloor}^{\lfloor\frac{m}{p}\rfloor} * C_{n\%p}^{ ...

  2. vue.js 组件共用函数的方法之一

    如果我现在写一个组件pullMore,想要用到loadMore里面的方法(函数), 那么只需要在当前组件pullMore,script里面先引入组件import loadMore from './lo ...

  3. Awesome Flask Awesome

    A curated list of awesome Flask resources and plugins Awesome Flask Framework Admin interface Authen ...

  4. node.js版本管理(Win) --- nvm-window

    目录 1. 安装 2. 使用 1. 安装 去往Git链接:https://github.com/coreybutler/nvm-windows. 点击下载链接: 选择第一个nvm-noinstall. ...

  5. AQS与重入锁ReetrantLock原理

    一.AQS原理 AQS(AbstractQueuedSynchronizer)队列同步器是用来构建锁.同步组件的基础框架. AQS内部通过一个volatile int类型的成员变量state控制同步状 ...

  6. CSS:CSS 合法颜色值

    ylbtech-CSS:CSS 合法颜色值 1.返回顶部 1. CSS 颜色 可以用以下方法来规定 CSS 中的颜色: 十六进制色 RGB 颜色 RGBA 颜色 HSL 颜色 HSLA 颜色 预定义/ ...

  7. app自动化测试工具robotium

    robotium基于instramentation框架,可对app白盒黑盒测试,缺点是测试进程和被测进程需要在一个进程中,不能跨应用 白盒测试时,需要app源代码,在eclipse里新建android ...

  8. UVaLive 6853 Concert Tour (DP)

    题意:给定 n 个城市,m 个月,表示要在这 n 个城市连续 m 个月开演唱会,然后给定每个月在每个城市开演唱会能获得的利润,然后就是演唱会在不同城市之间调动所要的费用, 问你,怎么安排这 n 个演唱 ...

  9. C#基础之--线程、任务和同步:一、异步委托

    创建线程的一种简单方式是定义一个委托,并异步调用它.委托是方法的类型安全的引用. Delegate还支持异步地调用方法.在后台Delegate类会创建一个执行任务的线程. 为了说明委托的异步特性,从一 ...

  10. Mac和Unix的常用命令行指令

    更新:2017/05/03/02:05 更新: 2017/05/14/11:14 更新: 2017/09/05/16:15 增加rm -rf 强制删除文件夹内所有文件 更新: 2018/01/16 完 ...