leetcode166 Fraction to Recurring Decimal
思路:
模拟。
实现:
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的更多相关文章
- Leetcode166. Fraction to Recurring Decimal分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 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 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- [Swift]LeetCode166. 分数到小数 | Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- LeetCode Fraction to Recurring Decimal
原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...
随机推荐
- JUC包中的锁框架
JUC包中的锁,包括:Lock接口,ReadWriteLock接口,LockSupport阻塞原语,Condition条件,AbstractOwnableSynchronizer/AbstractQu ...
- calicoctl命令简介
背景 在calico中,有多种网络资源.以v1.6.1为例,网络资源包含:node,bgpPeer,hostEndpoint,workloadEndpoint,ipPool,policy,profil ...
- html格式
优美的代码编写方式是我们装逼的基础,在python中我们称优秀的代码为pythonic,无独有偶,html.css.js也都有着自己相比更优美的写法~ <!DOCTYPE html> &l ...
- xml的的特殊字符转义&
& ampersand 连接符 & " quotation 双引号 “ &apos apostrophe 单引号 ...
- HDU - 6016 Count the Sheep 二分图+思维
Count the Sheep 题意: 问题描述 开学翘课固然快乐,然而也有让呃喵抓狂的事,那当然就是考试了!这可急坏了既要翘课又想要打BC还要准备考试的呃喵. 呃喵为了准备考试没有时间刷题,想打BC ...
- c# 调用 webservices (转载)
.net 调用webservice 总结 最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice . 我们都知道,调用webserivice 最简单 ...
- tensorflow weight_variable going
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- sql #与$的区别
#将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的值是i ...
- 强大的在线web编辑器UEditor
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码. UEditor在线演示地址:http://u ...
- jzoj5984. 【北大2019冬令营模拟2019.1.1】仙人掌 (分块)
题面 题解 数据结构做傻了.jpg 考虑每一个节点,它的儿子的取值最多只有\(O(\sqrt {m})\)种,那么可以用一个双向链表维护儿子的所有取值以及该取值的个数,那么对儿子节点修改一个值就是\( ...