166. Fraction to Recurring Decimal (Math)
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)".
class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
//deal with overflow (eg: -2147483648/-1, -1/-2147483648)
long l_numerator = (long) numerator;
long l_denominator = (long) denominator;
//deal with negative
string ret = "";
int p = ; //pointer to the return string
if((l_numerator < && l_denominator > ) || (l_numerator > && l_denominator < )) {
ret += "-";
p++;
}
l_numerator = abs(l_numerator);
l_denominator = abs(l_denominator);
long quotient = l_numerator/l_denominator;
long residue = l_numerator%l_denominator;
unordered_map<int,int> res_pos_map;
stringstream ss;
string sTmp;
ss << quotient;
ss >> sTmp;
ret += sTmp;
p+=sTmp.size();
if(residue!=) {
ret += ".";
p++;
res_pos_map[residue]=p;
}
while(residue!=){
l_numerator = residue * ;
quotient = l_numerator/l_denominator;
residue = l_numerator%l_denominator;
ss.clear();
ss << quotient;
ss >> sTmp;
ret += sTmp;
p++;
if(res_pos_map.find(residue)==res_pos_map.end()){
res_pos_map[residue]=p;
}
else{
ss.clear();
ss << quotient;
ss >> sTmp;
ss.clear();
ss << residue*/l_denominator;
ss >> sTmp;
ret = ret.substr(,res_pos_map[residue]) + "(" + ret.substr(res_pos_map[residue]) + ")";
return ret;
}
}
return ret;
}
};
166. Fraction to Recurring Decimal (Math)的更多相关文章
- 【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 分数转换 --------- java
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 ...
- 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 in ...
- 166 Fraction to Recurring Decimal 分数到小数
给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如, 给出 分子 = 1, 分母 = 2,返回 "0.5". ...
随机推荐
- MyBatis举例以及连接数据库过程
第一:创建实体类 package entity; public class Emp { private int empno; private String ename; private String ...
- pass
空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/java中: if(true) ; //do nothing else { //do something } 1 ...
- fges
基本用法: fgets函数用来从文件中读入字符串.fgets函数的调用形式如下:fgets(str,n,fp):此处,fp是文件指针:str是存放在字符串的起始地址:n是一个int类型变量.函数的功能 ...
- ASP.NET资源大全-知识分享 【转载】
API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...
- exec 与文件描述符
参考http://blog.csdn.net/baoendemao/article/details/51638746 1:用法 exec 3<2.txt 以只读方式打开2.txt, ...
- Linux初级入门(第一次作业)
Linux初级入门 在本科期间学过一些Linux的简单命令,再次接触Linux不仅巩固了知识还学习到了很多新的东西. 什么是操作系统? 操作系统,英文名称Operating System,简称OS,是 ...
- istream不是std的成员
如果报错信息为:istream不是std的成员,那么有两种可能 1.没有包含iostream库文件 2.#ifndef 和#endif使用错误,致使包含的iostream的头文件没有被主函数包含
- 如何在myeclipse中安装spket插件
在web开发中,经常会遇到自动提示,比如jquery.extjs等,在myeclipse写这些代码时需要自动提示,就需要安装spket插件,具体方法见下面 工具/原料 myeclipse spke ...
- MySQL Point in Time Recovery the Right Way
In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...
- springmvc log4j配置
1. web.xml <!-- 加载Log4J 配置文件 --> <context-param> <param-name>log4jConfigLocation&l ...