166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
For example,
- Given numerator = 1, denominator = 2, return "0.5".
- Given numerator = 2, denominator = 1, return "2".
- Given numerator = 2, denominator = 3, return "0.(6)".
string fractionToDecimal(int numerator, int denominator) {
string result;
//deal with the `ZERO` cases
if (denominator == ){ return result; }
if (numerator == ) { return ""; }
//using long long type make sure there has no integer overflow
long long n = numerator;
long long d = denominator;
//deal with negtive cases
bool sign = ((float)numerator/denominator >= );
if ( n < ){ n = -n; }
if ( d < ){ d = -d; }
if (sign == false){
result.push_back('-');
}
long long remainder = n % d;
long long division = n / d;
ostringstream oss;
oss << division;
result += oss.str();
if (remainder == ){
return result;
}
//remainder has value, the result is a float
result.push_back('.');
//using a map to record all of reminders and their position.
//if the reminder appeared before, which means the repeated loop begin,
//then, get the place from map to insert "(".
//(In C++11, it's better to use unordered_map )
map<long long, int> rec;
for (int pos=result.size(); remainder!=; pos++, remainder=(remainder*)%d ) {
if (rec.find(remainder) != rec.end()) {
result.insert(result.begin()+rec[remainder], '(');
result.push_back(')');
return result;
}
rec[remainder] = pos;
result.push_back((remainder*)/d + '');
}
return result;
}
166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 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】166 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
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- [leetcode72]166. Fraction to Recurring Decimal手动实现除法
让人火大的一道题,特殊情况很多 不过也学到了: java中int类型的最大值的绝对值比最小值的绝对值小1 int最小值的绝对值还是负的,除以-1也是 这种时候最好转为long类型进行处理 long n ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
随机推荐
- ggplot2 legend图例的修改
ggplot2中的legend包括四个部分: legend.tittle, legend.text, legend.key, legend.backgroud.针对每一部分有四种处理方式: eleme ...
- [转自老马的文章]用MODI OCR 21种语言
作者:马健邮箱:stronghorse_mj@hotmail.com发布:2007.12.08更新:2012.07.09按照<MODI中的OCR模块>一文相关内容进行修订2012.07.0 ...
- poj 1064 (二分+控制精度) && hdu 1551
链接:http://poj.org/problem?id=1064 Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- s表达式和json表达式
s表达式 + 1 2 3普通表达式 1+2+3json表达式{ +:[1, 2, 3]}优点,一个运算符,无限个参数 s表达式 * (+ 1 2) 3普通表达式 1+(2*3)json表达式{ *:[ ...
- iOS - Plist 数据解析
前言 NS_AVAILABLE(10_6, 4_0) @interface NSPropertyListSerialization : NSObject 如果对象是 NSArray 或 NSDicti ...
- Linux vi 中移动光标 命令
移动光标 上:k nk:向上移动n行 9999k或gg可以移到第一行 G移到最后一行下:j nj:向下移动n行左:h nh:向左移动n列右:l nl:向右移动n列 w:光标以单词向前移动 nw:光标向 ...
- lab_c!
#include<stdio.h> hi() { printf("hello world!\n"); } int main() { hi(); int i = hi() ...
- [转载] tmux 使用指南
原文: https://danielmiessler.com/study/tmux/ tmux的用法和screen类似, 比screen好用一些, 不过需要单独安装
- maven入门
1.1.项目构建 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 项目构建过程包括 ...
- Android 监听键盘的弹起与收缩
Android 监听键盘的弹起与收缩 由于android不存在该监听的API 所以需要自己去处理 先上代码 /* android:windowSoftInputMode="stateAlwa ...