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 ...
随机推荐
- CentOS 7 (无盘安装)PXE服务器的搭建(失败求助版)
折腾了一天半,PXE无盘服务器以暂时失败而告终. 基本原理 1. 首先客户端主机需要支持PXE,大部分主板都支持. 2. PXE服务器需要安装DHCP.TFTP.FTP服务. 3. DHCP服务用来给 ...
- GMM及EM算法
GMM及EM算法 标签(空格分隔): 机器学习 前言: EM(Exception Maximizition) -- 期望最大化算法,用于含有隐变量的概率模型参数的极大似然估计: GMM(Gaussia ...
- 重命名myclipse中web项目名称的过程
1 打开 myclipse2 最顶层项目上右键,Refactor,新名,3 最顶层项目上右键,propterties, MyEclipse, Web, context-root: /新名4 替换 .c ...
- 创建XML
//创建XML XElement xelement = new XElement("request", new XElement("head", new XEl ...
- 基于jQuery的H5调试条
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- DOM 表单应用
1.大小写转换 <script> var a='aaa'; var b='AaA'; var c='BbB'; //alert(c.toLowerCase()); //把含有大写的字母转换 ...
- server-pc--------------->lspci,lsusb,meminfo等配置信息
安装yum install pciutils usbutils [root@server09 ~]# [root@server09 ~]# lspci00:00.0 Host bridge: Inte ...
- DBCP JAVA 连接池
package com.sinoglobal.db; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...
- Nexus4_文件名乱码
1. 官方的出厂映像 for Android4.4:occam-krt16s-factory-2006f418.tgz 2. 自己编译的 Android-4.4_r1 (AOSP on Mako) 映 ...
- LINUX一切皆文件
只要用过linux的筒子,或者保守点说接触到一些linux思想的同志肯定听说过这样一句话,在linux下,“一切皆是文件”! 不错,今天walfred将在快速上手linux设备驱动这一块,谈谈linu ...