原题地址:

https://oj.leetcode.com/problems/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、模拟除法(自己做一遍,就是笔算复杂除法的过程。)

1、记住你产生的新的被除数以及其index,可以就用一个map来记,为了直观些我加了个set。

2、对产生的新被除数进行判断,如果重复了(map中有了),说明是循环小数,结束循环,在index前面加个括号,当然尾巴括号也要补上。

3、小心负数,由于正负不对称,为了方便我引入了long long类型,这样就不用担心溢出和负数了。

全部代码:

class Solution {
public:
string fractionToDecimal(int numerator1, int denominator1) {
bool isNegti = false;
long long numerator = numerator1;
long long denominator = denominator1;
long long interger = numerator / denominator;
if (numerator * denominator < 0) {
isNegti = true;
interger = -interger;
}
numerator = numerator < 0 ? -numerator : numerator;
denominator = denominator < 0 ? -denominator :denominator;
long long next;
if ((next = numerator % denominator) == 0)
return isNegti == true ? "-" + strval(interger) : strval(interger);
string deci = ".";
unordered_set<long long> dict;
unordered_map<long long,long long> start;
unordered_set<long long>::iterator itDict;
unordered_map<long long,long long>::iterator itStart;
char nownum;
int index = 1;
while (next) {
itDict = dict.find(next);
if (itDict != dict.end()) {
itStart = start.find(next);
int len = itStart->second;
deci = deci.substr(0,len) + '(' + deci.substr(len) + ')';
break;
}
dict.insert(next);
start[next] = index ++;
nownum = (next * 10) / denominator + '0';
deci = deci + nownum;
next = (next * 10) % denominator;
}
return isNegti == true ? "-" + strval(interger) + deci : strval(interger) + deci;
} static string strval(long long num) {
char tmp[20];
char index = 0;
while (num >= 10) {
tmp[index ++] = (char)(num % 10 + '0');
num /= 10;
}
tmp[index] = (char)(num + '0');
string res = "";
for (int i = index; i >= 0; i --) {
res = res + tmp[i];
}
return res;
}
};

  

【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告的更多相关文章

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

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

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

  3. 【leetcode】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 弗洛伊德判环

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

  5. 【LeetCode】166. Fraction to Recurring Decimal

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

  6. 【LeetCode】592. Fraction Addition and Subtraction 解题报告(Python)

    [LeetCode]592. Fraction Addition and Subtraction 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuem ...

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

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

  8. 【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

    时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: ...

  9. 【原创】leetCodeOj --- Excel Sheet Column Title 解题报告

    题目地址: https://oj.leetcode.com/problems/excel-sheet-column-title/ 题目内容: Given a positive integer, ret ...

随机推荐

  1. Java Web Services (0) - Overview

    前言第1章 Web服务快速入门 1.1 Web服务杂项 1.2 Web服务有什么好处 1.3 Web服务和面向服务的架构 1.4 Web服务简史 1.4.1 从DCE/RPC到XML-RPC 1.4. ...

  2. Java线程状态及Thread类中的主要方法

    要想实现多线程,就必须在主线程中创建新的线程对象. 不论什么线程一般具有5种状态,即创建,就绪,执行,堵塞,终止. 创建状态: 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时 ...

  3. tracert路由跟踪命令分析判断

    可能有的会使用路由跟踪命令 ,可是却看不太明确显示出来的结果.结合我的来说明一下. (1)tracert命令介绍 tracert是路由跟踪命令,通过该命令的返回结果,能够获得本地到达目标主机所经过的网 ...

  4. POJ 3040 Allowance 贪心

    这题目的贪心思路还是有一点细节问题的. 还没有证明,据说是因为题目给的条件是每个价格是比它小的价格的倍数才能这么贪心的. 思路如下: 假设要给奶牛的钱为C 1)从大面值到小面值一次拿钱,能拿多少拿多少 ...

  5. 上mongodb创建一些吸取的经验教训指数

    想来接触mongodb它已经快一年了,对于其指数已经积累了很多的经验,知识,以这个夜黑风高的优势,放mongodb总结一番吧. 一,索引介绍 mongodb具有两类索引,分别为单键索引和复合索引. 1 ...

  6. Android中canvas.save()和canvas.restore()的使用

    自己定义控件时经常遇到重写View的Ondraw()方法,Ondraw()方法经常设计到save()和restore()这两个方法.这两个相互匹配出现的,作用是用来保存画布的状态和取出保存的状态的. ...

  7. ZooKeeper的安装、配置、启动和使用(一)——单机模式

    ZooKeeper的安装.配置.启动和使用(一)——单机模式 ZooKeeper的安装非常简单,它的工作模式分为单机模式.集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装.配置 ...

  8. 201215-03-19---cocos2dx内存管理--具体解释

    因为cocos2dx我们的使用c++写的,所以内存管理就是一个绕只是去的坎,这个你不懂内存仅仅懂业务逻辑的话,还玩什么c++,今天看了半天这个东西,事实上本质上是理解的,可是就是有一个过不去的坎,最终 ...

  9. BZOJ 刷题记录 PART 6

    [BZOJ2709]水的二分加验证.可是好像被读入萎到了... [BZOJ3229]强大的算法见此.被机房的一堆大神"推荐".于是被坑了...写了一个下午... [BZOJ3631 ...

  10. poj1243(经典dp)

    题目链接:http://poj.org/problem?id=1243 题意:让你猜一个物品的价格,猜低了或者猜高了都会提示你.G,L,表示你有G次机会猜一个数,如果猜错了,G会减少1次,如果你的错误 ...