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)".
    0.16
6 ) 1.00
0
1 0 <-- Remainder=1, mark 1 as seen at position=0.
- 6
40 <-- Remainder=4, mark 4 as seen at position=1.
- 36
4 <-- Remainder=4 was seen before at position=1, so the fractional part which is 16 starts repeating at position=1 => 1(6).
如果发现余数曾经出现过,则说明开始循环了,利用一个hash表记录余数出现的位置即可
注意INT_MIN转换成正数会溢出,故需要先把数字先转为long long int
注意余数也要去long long int,因为-1,2147483648这种情况下,余数在计算乘以10的过程中会溢出
 
 class Solution {
public:
string fractionToDecimal(int numerator, int denominator) { string ans="";
if(numerator==) return ""; long long int n=numerator;
long long int d=denominator;
n=abs(n);
d=abs(d); if(numerator<^denominator<) ans.push_back('-'); map<long long int,int> hash; ans+=to_string(n/d);
long long int rem=n%d;
if(rem!=) ans.push_back('.'); while(rem!=)
{
if(hash.find(rem)!=hash.end())
{
ans.insert(hash[rem],"(");
ans.push_back(')');
break;
} hash[rem]=ans.length(); ans+=to_string(rem*/d);
rem=(rem*)%d;
} return ans;
}
};

【leetcode】Fraction to Recurring Decimal的更多相关文章

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

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

  2. 【Leetcode 166】 Fraction to Recurring Decimal

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

  3. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

  4. [LeetCode] 167. Fraction to Recurring Decimal 分数转循环小数

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

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

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

  6. Java for LeetCode 166 Fraction to Recurring Decimal

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

  7. Leetcode#166 Fraction to Recurring Decimal

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

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【LeetCode】哈希表 hash_table(共88题)

    [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target ...

随机推荐

  1. 使用phpize增加php模块

    一,phpize的好处 什么时候我们要用phpize呢?我们在安装php时: ./configure --prefix=/apps/product/php --with-config-file-pat ...

  2. redis入侵小结

    redis安装: windows安装包:http://pan.baidu.com/s/1i3jLlC5 下载下来之后,开始安装: redis-server.exe redis.conf: 简单一步,安 ...

  3. VisualStudio基本使用(2)-使用VS2013来编译C语言程序

    切换到C语言程序所有目录,执行cl test.c

  4. [MongoDB]mapReduce

    摘要 上篇文章介绍了count,group,distinct几个简单的聚合操作,其中,group比较麻烦一点.本篇文章将学习mapReduce的相关内容. 相关文章 [MongoDB]入门操作 [Mo ...

  5. 【8-23】MFC学习笔记 01

    太难了,慢慢学....

  6. thinkphp-许愿墙-2

    在数组中,也可以使用函数,如: $data = array( 'username'=> I('username','', 'htmlspecailchars'), 'content'=> ...

  7. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  8. Emmet语法实例(帮助快速开发)

    写完命令后按键 tab 就可以生成了. 应用于大多数已经内置或可以安装emmet的编辑器下级元素命令 > <!--div>p--> <div> <p>& ...

  9. ACM3 求最值

    /*2*2014.11.18*求最值*描述:给定N个整数(1<=N<=100),求出这N个数中的最大值,最小值.*输入:多组数据,第一行为一个整数N,第二行为N个不超过100的正整数,用空 ...

  10. js改变HTML元素的值

    js改变HTML元素的值(常用,备忘) <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h ...