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". ...
随机推荐
- zombodb 几个方便的_cat api
zombodb 暴露所有es _cat/ api 为视图,我们可以通过视图方便的查询es 的信息,默认在zdb的schema 中 包含的视图 几个方便的view 查看索引统计信息zdb.index_s ...
- Sublime远程连接Linux
1:Ctrl+Shift+P,输入install 选择红框框然后Enter 2:输入ftp,然后找到sftp,Enter 3:修改配置 修改账号密码信息远程路径之后 ...
- Day 09 函数基础
函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...
- Eclipse Java EE IDE for Web Developers集成的Maven 3 指向自己安装的 Maven
一.配置Maven环境 1.下载apache-maven文件,选择自己需要的版本,地址:Apache 官方下载地址是http://maven.apache.org/download.cgi 2.下载并 ...
- Property referenced in indexed property path is neither an array nor a List nor a Map
记一次传参请求报错,没有解决 Invalid property 'distributeCars[0][ackStatus]' of bean class [com.api6.plate.prototy ...
- RouterOS限速更高级运用
转自这里 一般我们用ros限速只是使用了max-limit,其实ros限速可以更好的运用.比如我们希望客户打开网页时速度可以快一些,下载时速度可以慢一些.ros2.9就可以实现. 看图片 max-li ...
- 【python】脚本连续发送QQ邮件
今天习得用python写一个连续发送QQ邮件的脚本,经过测试,成功给国内外的服务器发送邮件,包括QQ邮箱.163邮箱.google邮箱,香港科技大学的邮箱和爱丁堡大学的邮箱.一下逐步解答相关技巧. 首 ...
- Android Gradle 依赖方式
Android Gradle 依赖方式有以下6种: Compile compile是对所有的build type以及favlors都会参与编译并且打包到最终的apk文件中. Provided Prov ...
- 部署MySQL自动化运维工具inception+archer
***************************************************************************部署MySQL自动化运维工具inception+a ...
- bzoj2141排队
/* 动态求逆序对,可以树套树来写, 将交换操作理解成插入和删除比较好理解, 里层是个区间求和的线段树就好了 或者叫 带修主席树? */ #include<cstdio> #include ...