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

不过也学到了:

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. 010 editor的使用

    原文链接:http://www.cnblogs.com/vendanner/p/4939444.html 注意事项:之前一直在虚拟机winxp中添加template一直失败,原因可能是因为虚拟机的版本 ...

  2. Spring Cloud 学习 (三) Feign

    新建 spring-cloud-eureka-feign-client Module pom <parent> <artifactId>spring-cloud-parent& ...

  3. Java 命名之道

    为什么需要重视命名? 好的命名即是注释,别人一看到你的命名就知道你的变量.方法或者类是做什么的! 好的命名对于其他人(包括你自己)理解你的代码有着很大的帮助! 简单举个例子说明一下命名的重要性. &l ...

  4. PyQt(Python+Qt)学习随笔:QDateTimeEdit日期时间编辑部件

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 Designer输入部件中,Date/Time E ...

  5. PyQt(Python+Qt)学习随笔:QLineEdit行编辑器功能详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.概述 QLineEdit部件是一个单行文本编辑器,支持撤消和重做. ...

  6. Github 美化设置个人主页

    起因是发现自己follow的大师傅个人主页跟普通的不太一样: 猜测应该是Github啥时候出现的新功能,查了一下,发现可以通过创建同名仓库来实现个人主页的美化设置 首先在 GitHub 上建立一个与自 ...

  7. UOJ61. 【UR #5】怎样更有力气

    题目链接 Statement 给定一棵 \(n\) 点树 \(T\) 和 \(m\) 个操作 v u w : 在 \(T\) 中 \(u,v\) 的最短路上所有点里面选出若干对(可以不选,可以重复), ...

  8. Jmeter(4)断言

    Jmeter添加断言,检查测试中得到的响应数据是否符合预期.以下介绍下响应断言,JSON断言 一.响应断言 1.创建测试计划: 添加线程组->添加取样器->添加察看结果树,运行后可查看接口 ...

  9. MySQL锁(一)全局锁:如何做全库的逻辑备份?

    数据库锁设计的初衷是处理并发问题,这也是数据库与文件系统的最大区别. 根据加锁的范围,MySQL里大致可以分为三种锁:全局锁.表锁和行锁.接下来我们会分三讲来介绍这三种锁,今天要讲的是全局锁. 全局锁 ...

  10. js上 十三、函数初步-2

    13-1.函数的参数 函数的本质: ü 函数的作用,代码重用,编写一个函数,就是为了解决一类问题. ü 函数每次调用,都有一个结果,那么结果和什么相关呢?y = x , y = x2,y = sin( ...