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. [HDOJ5783]Divide the Sequence(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5783 题意:给n个数,要求划分成多个段,使得每一个段的任意前缀和都不小于0. 从后往前截取,这样不会影 ...

  2. python基础字符串操作

    去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sS ...

  3. 草珊瑚的css基础

    首先要了解如下概念: viewport,窗口大小,containing block,block formatting context,inline formatting context,dirctio ...

  4. 使用@ContextConfiguration注解后,提示找不到配置文件

    intellij提示找不到配置文件 错误代码如下: 严重: Caught exception ] java.lang.IllegalStateException: Failed to load App ...

  5. yii 常用路径

    yii::app()->homeurl //主页的网址 yii系统变量. //得到proteced目录的物理路径 Yii::app()->basePath; 调用YII框架中jquery: ...

  6. Android Fast ImageLoader

    前段时间写的Android平台开源项目:Fast ImageLoader,现在分享给大家 源码地址:https://github.com/cumtkangyi/Android-Fast-ImageLo ...

  7. javascript学习-原生javascript的小特效(简单的运动效果)

    前些日子看了个视频所以就模仿它的技术来为大家做出几个简单的JS小特效 一:运动特效(主要是通过改变元素的left,right,height,width,opacity来达到运动的效果) 我们今天做一个 ...

  8. python中的for循环

    打印出1到100的数,不包含100 for i in range(1,100): if i==23: print "great,you got your luncky number:&quo ...

  9. android设备休眠机制

    如果一开始就对Android手机的硬件架构有一定的了解,设计出的应用程序通常不会成为待机电池杀手,而要设计出正确的通信机制与通信协议也并不困难.但如果不去了解而盲目设计,可就没准了. 首先Androi ...

  10. asc.desc

    DESC 是descend 降序意思 asc 是ascend 升序的意思