原题地址:

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. CentOS 6.4下编译安装MySQL 5.6.16

    一.卸载旧版本号MySql 1.rpm卸载: 1> 检查安装包: rpm -qa | grep mysql 2> 普通删除: rpm -e mysql-5.6.16.rpm 3> 强 ...

  2. codeforces 597B Restaurant

    题目链接:http://codeforces.com/contest/597/problem/B 题目分类:贪心 题目分析:经典的看节目问题(挑战程序设计page 40) 代码: #include&l ...

  3. hdu 4963(中途相遇法)

    题目链接:Dividing a String 题意:给定一个2*n(n<=20)的字符串及每个位置的字符包含的权重,求将该字符串分成两个子序列S1.T1,要求S1=T1且abs(weight1- ...

  4. newlisp 注释生成文档

    最近写了一个newlisp_armory库,用来实现一些newlisp自身不支持的操作.比如跨windows和ubuntu的目录拷贝功能等. 自己用的时候,发现没有API reference文档参考, ...

  5. Tiny server:小型Web服务器

    一.背景 csapp的网络编程粗略的介绍了关于网络编程的一些知识,在最后的一节主要就实现了一个小型的Webserver.这个server名叫Tiny,它是一个小型的可是功能齐全的Webserver.在 ...

  6. expression:stream!=NULL

    如果fopen()后返回的是NULL:就不能调用fclose()了: 用fopen()获得的文件句柄不是NULL,那么就需要用fclose()来关闭它.如果是NULL则不需要 null就表示你打开文件 ...

  7. Oracle“记录被另一个用户锁住” 无法更新删除的解决办法

    1.查看数据库锁,诊断锁的来源及类型: select object_id,session_id,locked_mode from v$locked_object; 或者用以下命令: select b. ...

  8. CSDN博文大赛赛况简报

    CSDN博文大赛已经開始两周啦.如今赛况怎样呢,接下来,小编为大家揭晓. 大赛自2014年6月10日正式开赛以来.博友们踊跃发表文章,提交文章.到眼下为止,博主们提交博文1045余篇.且以上这些数据还 ...

  9. 为什么 as sysdba着陆方法oracle数据库,为什么刚刚输入username和password我们都可以登录?

    事实上,这是oracle问题数据库的身份验证方法 该 sqlnet.ora在文件 SQLNET.AUTHENTICATION_SERVICES= (NTS) 变 SQLNET.AUTHENTICATI ...

  10. 什么是Spring?Spring是什么?

    Spring概述: Spring是一个开源框架,是为了解决企业应用程序开发复杂性而开发的. 从简单性.可測试性和松耦合的角度而言,不论什么java应用都能够从Spring中受益. 简而言之,Sprin ...