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

不过也学到了:

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

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

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

  1. long num = (long)numerator;
  2. long den = (long)denominator;
  3. //两种特殊情况,一种是分母为0,一种是可以直接处尽
  4. if (den==0)
  5. return "";
  6. if (num%den==0)
  7. return num/den+"";
  8. //记录结果
  9. StringBuilder res = new StringBuilder();
  10. //判断商是负数的情况,在结果前边添加负号
  11. if ((num<0&&den>0)||(num>0&&den<0))
  12. res.append("-");
  13. //全部化成正数,不然会每位都有负号
  14. num = Math.abs(num);
  15. den = Math.abs(den);
  16. //添加上整数部分和.
  17. res.append(num/den+".");
  18. //记录当前余数
  19. long temp = num%den;
  20. //记录每次分子对应的那一位结果的index和用来判断是否开始循环
  21. Map<Long,Integer> map = new HashMap<>();
  22. int sta = res.length();
  23. while (temp!=0)
  24. {
  25. //如果开始循环,跳出
  26. if (map.containsKey(temp))
  27. break;
  28. //如果不是循环,那就记录位置
  29. map.put(temp,sta);
  30. //记录此次相除的结果
  31. res.append(temp*10/den);
  32. //更新余数
  33. temp= (((long)(temp*10))%den);
  34. //位置前移
  35. sta++;
  36. }
  37. //有两种情况会跳出循环,一种是没有余数了,直接返回,一种是商是循环小数
  38. if (temp==0)
  39. return new String(res);
  40. //查询开始循环的位置
  41. int left = map.get(temp);
  42. res.insert(left,"(");
  43. res.append(")");
  44. 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. MySQL 连接为什么挂死了

    声明:本文为博主原创文章,由于已授权部分平台发表该文章(知乎.云社区),可能造成发布时间方面的困扰. 一.背景 近期由测试反馈的问题有点多,其中关于系统可靠性测试提出的问题令人感到头疼,一来这类问题有 ...

  2. JMeter 安装 启动(即中文的修改)

    一.安装 (1).java 和 apache-jmeter-4.0 2.点击apache-jmeter-4.0进入bin目录,点击jmeter.bat 如果没有安装java就会出现下图 遇到上面是因为 ...

  3. NOIP2020退役记

    Day 0 这一次因为疫情仍然没有试机,但是允许提前打开虚拟机,减少了调试虚拟机的时间 开考时,吸取上一次 \(CSP\) 的教训,先把题面看了一遍 感觉 \(T1\) 比较可做,剩下的暂时没有思路 ...

  4. [Windows] Prism 8.0 入门(上):Prism.Core

    1. Prism 简介 Prism 是一个用于构建松耦合.可维护和可测试的 XAML 应用的框架,它支持所有还活着的基于 XAML 的平台,包括 WPF.Xamarin Forms.WinUI 和 U ...

  5. moviepy音视频剪辑:使用fl_time进行时间特效处理报错OSError: Error in file xxxx, Accessing time

    ☞ ░ 前往老猿Python博文目录 ░ 老猿在使用moviepy音视频剪辑的fl_time进行时间特效处理时,系统报错: OSError: Error in file F:\video\WinBas ...

  6. 第9.11节 Python中IO模块文件打开读写操作实例

    为了对前面学习的内容进行一个系统化的应用,老猿写了一个程序来进行文件相关操作功能的测试. 一. 测试程序说明 该程序允许测试人员选择一个文件,自己输入文件打开模式.写入文件的位置以及写入内容,程序按照 ...

  7. 第14.18节 爬虫实战4: request+BeautifulSoup+os实现利用公众服务Wi-Fi作为公网IP动态地址池

    写在前面:本文相关方法为作者独创,仅供参考学习爬虫技术使用,请勿用作它途,禁止转载! 一. 引言 在爬虫爬取网页时,有时候希望不同的时候能以不同公网地址去爬取相关的内容,去网上购买地址资源池是大部分人 ...

  8. C# Email 帮助类 EmailHelper

    1. 配置文件 App.config <?xml version="1.0" encoding="utf-8" ?> <configurati ...

  9. WordCount个人项目

    1.GitHub地址:https://github.com/lyh27/WordCount 2.题目描述 Word Count1. 实现一个简单而完整的软件工具(源程序特征统计程序).2. 进行单元测 ...

  10. java中==和equals的不同使用方法

    System.out.println("input a charact a      "); Scanner input2 = new Scanner(System.in); St ...