思路:

模拟。

实现:

 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. C# 使用WebClient时,在多网卡时,指定IP发送Web请求

    需要定义一个类,重写GetWebRequest,在方法内,指定IP地址 public class MyWebClient : WebClient { private IPAddress ipAddre ...

  2. stdout引发的curl 302跳转 crash

    重现步骤: 0.开发环境:Windows 7 SP1 64bit, VS2008 SP1 1.进程中修改了stdout这个句柄的值:调用了prinft和cout都会修改stdout,TRACE不会修改 ...

  3. MyEclipse10.0安装SVN的三种方法

    最简单的一种: 首先下载zip包(svn:http://subclipse.tigris.org) 1.直接在MyEclipse10安装目录下的dropins文件夹下新建一个svn文件夹 2.把解压出 ...

  4. jQuery 生成随机字符

    //获取长度为len的随机字母 //获取长度为len的随机字母 function zimu(len){ len = len || 1; var $chars = 'ABCDEFGHIJKLMNOPQR ...

  5. node代理网络请求

    server.js var express = require('express') var axios = require('axios') var port = 9000; var app = e ...

  6. 简单介绍Git两种拉取代码的方式

    first: 1.通过git clone 命令克隆git库中的项目 注意:通过 git clone方式克隆的代码会在服务器上自动建一个与git库名相同的文件夹,所以有两种思路,第一种就是直接在wwwr ...

  7. [Xcode 实际操作]二、视图与手势-(11)UITapGestureRecognizer手势之长按

    目录:[Swift]Xcode实际操作 本文将演示使用视图的长按手势,完成视图的交互功能. import UIKit class ViewController: UIViewController { ...

  8. [Xcode 实际操作]八、网络与多线程-(5)使用UIApplication对象发送邮件

    目录:[Swift]Xcode实际操作 本文将演示如何使用应用程序单例对象,发送邮件的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] 注:需要使用真机进行测 ...

  9. kafka 安装部署

    环境:ubuntu 12.04 64位桌面版 解压kafka -0.10.0.0.tgz -C /root/software/ 进入目录 cd kafka_2.-0.10.0.0/ 创建data 目录 ...

  10. js对象—类型和属性特性

    前言 权威指南中摘要的,工作中用不到的,重要的js基础. 三类对象两类属性 内置对象(native object) 是由ECMScript规范定义的对象或者类.例如:函数,数组,日期,正则... 宿主 ...