让人火大的一道题,特殊情况很多

不过也学到了:

java中int类型的最大值的绝对值比最小值的绝对值小1

int最小值的绝对值还是负的,除以-1也是

这种时候最好转为long类型进行处理

long num = (long)numerator;
long den = (long)denominator;
//两种特殊情况,一种是分母为0,一种是可以直接处尽
if (den==0)
return "";
if (num%den==0)
return num/den+"";
//记录结果
StringBuilder res = new StringBuilder();
//判断商是负数的情况,在结果前边添加负号
if ((num<0&&den>0)||(num>0&&den<0))
res.append("-");
//全部化成正数,不然会每位都有负号
num = Math.abs(num);
den = Math.abs(den);
//添加上整数部分和.
res.append(num/den+".");
//记录当前余数
long temp = num%den;
//记录每次分子对应的那一位结果的index和用来判断是否开始循环
Map<Long,Integer> map = new HashMap<>();
int sta = res.length();
while (temp!=0)
{
//如果开始循环,跳出
if (map.containsKey(temp))
break;
//如果不是循环,那就记录位置
map.put(temp,sta);
//记录此次相除的结果
res.append(temp*10/den);
//更新余数
temp= (((long)(temp*10))%den);
//位置前移
sta++;
}
//有两种情况会跳出循环,一种是没有余数了,直接返回,一种是商是循环小数
if (temp==0)
return new String(res);
//查询开始循环的位置
int left = map.get(temp);
res.insert(left,"(");
res.append(")");
return new String(res);

[leetcode72]166. Fraction to Recurring Decimal手动实现除法的更多相关文章

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

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

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

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

  3. 【LeetCode】166. 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

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

  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. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

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

  8. 166. Fraction to Recurring Decimal

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

  9. 166. Fraction to Recurring Decimal (Math)

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

随机推荐

  1. Spring Boot + JPA 多模块项目无法注入 JpaRepository 接口

    问题描述 Spring Boot + JPA 多模块项目,启动报异常: nested exception is org.springframework.beans.factory.NoSuchBean ...

  2. zookeeper基础笔记

    一.安装 1.安装jdk 2.安装Zookeeper 3.单机模式(stand-alone):安装目录/conf   复制 zoo_sample.cfg 并粘贴到当前目录下,命名zoo.cfg. 二. ...

  3. PyQt开发案例:结合QDial实现的QStackedWidget堆叠窗口程序例子及完整代码

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.案例说明 本案例是老猿在学习QStackedWidget中的一个测试案例,该案例使用QStack ...

  4. IKUN python 反序列化

    题目过程1.一开始提示说要买到V6,观察源码,发现/static/img/lv/lv4.png.注册之后尝试寻找V6.观察url发现/shop?page=2.尝试写脚本匹配一下.发现在第181页. i ...

  5. Nmap学习使用指南

    本博客严重参考 Nmap使用指南1.0: https://github.com/scanfsec/penetration/blob/master/Nmap%E4%BD%BF%E7%94%A8%E6%8 ...

  6. 几句话说明 .NET MVC中ViewData, ViewBag和TempData的区别

    ViewData和TempData是字典类型,赋值方式用字典方式, ViewData["myName"] ViewBag是动态类型,使用时直接添加属性赋值即可 ViewBag.my ...

  7. go学习的第7天

    不容易啊,坚持7天了呢,今天开始看视频学习 https://www.bilibili.com/video/BV1pt41127FZ?from=search&seid=4441824587572 ...

  8. 一个不倒翁App

  9. EF 查询外键对应的实例

    EF 查询外键对应的实例   1. 查询时易遇到的情况: 能查询到外键值,但对应的外键实例为null. 解决方法: (1) 使用EF的include // 我的应用如下 // SampleResult ...

  10. sqlplus、lsnrctl命令工具不可用(libclntsh.so.11.1)

    原因: libclntsh.so.11.1文件丢失了 解决方法: 在其他机器把这个文件拷贝到目标库安装目录底下的lib目录即可