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

Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.

Hide Tags

Hash Table Math

 

 
  题目其实挺容易的,需要考虑的是溢出问题,long int 长度是32 位的, long long int 才是64位。
 
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
using namespace std; class Solution {
public:
string fractionToDecimal(int numerator, int denominator) {
bool flag1 = numerator<;
bool flag2 = denominator<;
unsigned numer = flag1?-numerator:numerator;
unsigned denom = flag2?-denominator:denominator; if(denom==) return "";
if(numer==) return "";
stringstream ss;
ss<<numer/denom;
string ret = ss.str();
unsigned long long int lft = numer%denom; unordered_map<unsigned int,unsigned int> mp;
string ret1="";
unsigned int cnt = ;
while(lft){
if(mp[lft]==){
mp[lft]=cnt++;
ret1 += (lft*)/denom + '';
lft = (lft*)%denom;
continue;
}
ret1 = ret1.substr(,mp[lft]-) + "(" + ret1.substr(mp[lft]-) + ")";
break;
}
if(ret1 != "") ret += "." + ret1;
if(flag1^flag2) ret = "-" + ret;
return ret;
}
}; int main()
{
Solution sol;
cout<<sol.fractionToDecimal(-,-)<<endl;
return ;
}

[LeetCode] Fraction to Recurring Decimal 哈希表的更多相关文章

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

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

  2. LeetCode Fraction to Recurring Decimal

    原题链接在这里:https://leetcode.com/problems/fraction-to-recurring-decimal/ 题目: Given two integers represen ...

  3. 【leetcode】Fraction to Recurring Decimal

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

  4. 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 ...

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

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

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

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

  7. 【LeetCode】166. Fraction to Recurring Decimal

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

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

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

  9. LeetCode(166) Fraction to Recurring Decimal

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

随机推荐

  1. JQuery官方学习资料(译):使用JQuery的.index()方法

        .index()是一个JQuery对象方法,一般用于搜索JQuery对象上一个给定的元素.该方法有四种不同的函数签名,接下来将讲解这四种函数签名的具体用法. 无参数的.index() < ...

  2. crossplatform---Node.js Applications with VS Code

    Node.js is a platform for building fast and scalable server applications using JavaScript. Node.js i ...

  3. atitit.元编程总结 o99

    atitit.元编程总结 o99.doc 1. 元编程(Metaprogramming) 1 2. 元编程的历史and发展 1 3. 元类型and元数据 1 4. 元编程实现方式 2 4.1. 代码生 ...

  4. paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决

    paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决 windows ok linux犯错误... 查看loging, 初始化的时候儿jdbcurl,user,pwd都是 ...

  5. VC基于消息的异步套接字

    用WSAStartup,需要在StdAfx.h头文件中需要声明 #include    #pragma   comment(lib,"WS2_32.lib") 用AfxSocket ...

  6. Oracle数据库恢复

    建用户 wf2014 赋权限 grant dba to wf2014; 数据恢复 imp wf2014/wf2014 file=D:\wf2014.dmp full=y 参数设置: datasourc ...

  7. 【VerySky原创】如何查找SNRO编号范围的使用情况;

    SAP所有编号范围的对象都可以在表NRIV中找到:

  8. Meanshift filter实现简单图片的卡通化效果

        利用Meanshift filter和canny边缘检测的效果,可以实现简单的图片的卡通化效果.简单的说,就是用Meanshift filter的结果减去canny算法的结果得到卡通化的效果. ...

  9. spring scope="prototype", scope="session"

    转自: http://www.cnblogs.com/JemBai/archive/2010/11/10/1873954.html struts+spring action应配置为scope=&quo ...

  10. mac os x安装ngigx+php fastcgi+mysql+memcache详细流程

    Part 1: MacPorts Mac上装软件常用的是MacPorts和homebrew,这个软件会很方便地提供软件的安装.装这些前先得装Xcode,Xcode在appstore上有,一个多G,下载 ...