Description:

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)".

Solution:

  long division: 长除法

  Trick: Determining whether two nums have different sign(+ or  -) can use follow codes:

if((n<)^(d<))  //异号
or
if((n>)^(d>)) //异号

  Above code avoiding product's value overflow.

Code:

class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
if(!numerator) return "";
long long n = numerator, d = denominator;
string ret = "";
if((n<)^(d<)) ret += '-';
if(n < ) n = -n;
if(d < ) d = -d;
ret += to_string(n/d);
if(n % d == ){
return ret;
}ret += '.';
map<long long, int>hash;
for(long long r = n % d; r; r %= d){
if(hash.find(r) != hash.end()){
ret.insert(hash[r],"(");
ret += ")";
return ret;
}
hash[r] = ret.size();
r *= ;
ret += to_string(r / d);
}
}
};

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

  1. 【leetcode】Fraction to Recurring Decimal

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

  2. LeetCode(166) Fraction to Recurring Decimal

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

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

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

  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

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

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

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

  7. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  8. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. JavaSE 学习笔记之网络编程(二十三)

    端口: 物理端口: 逻辑端口:用于标识进程的逻辑地址,不同进程的标识:有效端口:0~65535,其中0~1024系统使用或保留端口. java 中ip对象:InetAddress. import ja ...

  2. HDU 1234 简单模拟题

    题目很简单不多说了,我只是觉得这题目的输入方式还是很有特点的 #include <cstdio> #include <cstring> #include <algorit ...

  3. [luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)

    传送门 有向图,找点数大于1的强连通分量个数 ——代码 #include <stack> #include <cstdio> #include <cstring> ...

  4. noip模拟赛 兔子

    [问题描述]在一片草原上有N个兔子窝,每个窝里住着一只兔子,有M条路径连接这些窝.更特殊地是,至多只有一个兔子窝有3条或更多的路径与它相连,其它的兔子窝只有1条或2条路径与其相连.换句话讲,这些兔子窝 ...

  5. [bzoj4987]Tree_树形dp

    Tree bzoj-4987 题目大意:给定一颗n个点的有边权的树,选出k个点,使得:$\sum\limits_{i=1}^{k-1}dis_idis_j$最小. 注释:$1\le n\le 3000 ...

  6. SQL Server 性能优化实战系列(文章索引) : 桦仔

    http://www.cnblogs.com/gaizai/archive/2012/01/20/2327814.html

  7. 符号变换引擎(Symbol Transform Engine - STE)

    在写编译器的过程中.我意识到编译事实上是一种符号变换,比方C语言编译成机器码,事实上是C源代码文件里的符号变换成EXE的16进制符号,这和中文翻译成英语的语言翻译器没什么差别. 每一个程序猿都有自己喜 ...

  8. 此人牛b,学习榜样

    http://liyangliang.me/about/ ------------------------------------------------------------2017年11月12日 ...

  9. C#中类的详解

    类定义的具体语法形式类的访问修饰符 修饰符 类名{ 类的成员} 类的访问修饰符:用于设定对类的访问权限,包括public.internal或者不写,用internal或者不写时代表只能在当前项目中访问 ...

  10. Apache配置基于域名的虚拟主机

    一.设定 模拟域名  www.wang.org.blog.wang.org.bbs.wang.org 网站文件夹 /var/html/www./var/html/blog./var/html/bbs ...