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)的更多相关文章

  1. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  2. Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环

    分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...

  3. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  4. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  5. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  6. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  7. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  8. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  9. 166 Fraction to Recurring Decimal 分数到小数

    给定两个整数,分别表示分数的分子和分母,返回字符串格式的小数.如果小数部分为循环小数,则将重复部分括在括号内.例如,    给出 分子 = 1, 分母 = 2,返回 "0.5".  ...

随机推荐

  1. 2018.5.2 file结构体

    f_flags,File Status Flag f_pos,表示当前读写位置 f_count,表示引用计数(Reference Count): dup.fork等系统调用会导致多个文件描述符指向同一 ...

  2. springMvc---跨服务器文件上传(实测总结)

    序言: 该案例是采用springMvc实现跨服务器图片上传功能,其中用到的主要类和工具有:CommonsMultipartResolver.jquery.form.js.如果要实现多个文件上传,只需要 ...

  3. Python开发 基礎知識 (未完代補)

    一.Python基本知識 1.Python屬高階語言,所編築的是字節碼 2.一般狀態statement 終止於換行,如需使用多數行編寫,可在行末加上 \,以表延續 但在 parentheses ( ) ...

  4. LG1912 [NOI2009]诗人小G

    题意 题目描述 小G是一个出色的诗人,经常作诗自娱自乐.但是,他一直被一件事情所困扰,那就是诗的排版问题. 一首诗包含了若干个句子,对于一些连续的短句,可以将它们用空格隔开并放在一行中,注意一行中可以 ...

  5. 使用maven-tomcat7-plugins时调试出现source not found解决

    直接看下面的步骤: 步骤1: 步骤2: 步骤3: 步骤4:

  6. centos7.1安装tomcat8

    上传软件包 [root@linux-node1 tools]# ls apache-tomcat-8.0.32.tar.gz jdk-8u74-linux-x64.tar.gz 添加普通用户tomca ...

  7. WinFormEx

    项目地址 :  https://github.com/kelin-xycs/WinFormEx WinFormEx 一个 用 C# 写的 WinForm 扩展库 , 用于改善 WinForm 的 界面 ...

  8. 请确保 ASP.NET State Service (ASP.NET 状态服务)已启动 问题解决

    当iis部署的网站访问遇到如下错误时: 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同.如 ...

  9. centos 使用RPM包安装指定版本的docker-engine

    下面是拿安装docker-engine-1.10.3-1为例: wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docke ...

  10. js源生ajax

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...