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 -- 将除法的商表示成字符串(循环节用括号表示)的更多相关文章

  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

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  6. [leetcode72]166. Fraction to Recurring Decimal手动实现除法

    让人火大的一道题,特殊情况很多 不过也学到了: java中int类型的最大值的绝对值比最小值的绝对值小1 int最小值的绝对值还是负的,除以-1也是 这种时候最好转为long类型进行处理 long n ...

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

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

  8. Java for LeetCode 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

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

随机推荐

  1. ubuntu下ssh登陆阿里云服务器(ubuntu系统)中文乱码问题

    研究了几天终于解决了... 原文地址:  http://blog.csdn.net/a__yes/article/details/50489456 问题描述: 阿里云的服务器ubuntu系统,wind ...

  2. [转老马的文章]MODI中的OCR模块

    作者:马健邮箱:stronghorse_mj@hotmail.com发布:2012.07.02更新:2012.07.09补充非简体中文版内容 自从基于MODI的DjVuToy.FreePic2Pdf. ...

  3. Java自制人机小游戏——————————剪刀、石头、布

    package com.hello.test; import java.util.Scanner; public class TestGame { public static void main(St ...

  4. css 背景色渐变---和背景色透明

    1 背景色渐变 background:#fb0000; background: -webkit-gradient(linear, left top, left bottom, color-stop(0 ...

  5. IO端口和IO内存的区别及分别使用的函数接口

    每个外设都是通过读写其寄存器来控制的.外设寄存器也称为I/O端口,通常包括:控制寄存器.状态寄存器和数据寄存器三大类.根据访问外设寄存器的不同方式,可以把CPU分成两大类.一类CPU(如M68K,Po ...

  6. (三)uboot源码分析

    一.九鼎官方uboot和三星原版uboot对比(1)以九鼎官方的uboot为蓝本来学习的,以三星官方的这份为对照.(2)不同版本的uboot或者同一版本不同人移植的uboot,可能目录结构和文件内容都 ...

  7. 【转载】高性能IO设计 & Java NIO & 同步/异步 阻塞/非阻塞 Reactor/Proactor

    开始准备看Java NIO的,这篇文章:http://xly1981.iteye.com/blog/1735862 里面提到了这篇文章 http://xmuzyq.iteye.com/blog/783 ...

  8. Android 中Service生命周期

    使用context.startService() 启动Service 其生命周期为context.startService() ->onCreate()- >onStart()->S ...

  9. [html] Quirks Standards

    [产生] Standards : 从IE6开始,引入了Standards模式 Quirks  : 在IE6之前CSS还不够成熟,所以IE5等之前的浏览器对CSS的支持很差,为了兼容ie5之前的页面, ...

  10. D3.js 让图表动起来

    D3 支持制作动态的图表.有时候,图表的变化需要缓慢的发生,以便于让用户看清楚变化的过程,也能给用户不小的友好感. 一.什么是动态效果 绘制完成后不再发生变化的,这是静态的图表. 动态的图表,是指图表 ...