[leetcode72]166. Fraction to Recurring Decimal手动实现除法
让人火大的一道题,特殊情况很多
不过也学到了:
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手动实现除法的更多相关文章
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6) 弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人.代码中one就是速度1的人,而two就是速度为2的人. ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- 166. Fraction to Recurring Decimal (Math)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
随机推荐
- MySQL 连接为什么挂死了
声明:本文为博主原创文章,由于已授权部分平台发表该文章(知乎.云社区),可能造成发布时间方面的困扰. 一.背景 近期由测试反馈的问题有点多,其中关于系统可靠性测试提出的问题令人感到头疼,一来这类问题有 ...
- JMeter 安装 启动(即中文的修改)
一.安装 (1).java 和 apache-jmeter-4.0 2.点击apache-jmeter-4.0进入bin目录,点击jmeter.bat 如果没有安装java就会出现下图 遇到上面是因为 ...
- NOIP2020退役记
Day 0 这一次因为疫情仍然没有试机,但是允许提前打开虚拟机,减少了调试虚拟机的时间 开考时,吸取上一次 \(CSP\) 的教训,先把题面看了一遍 感觉 \(T1\) 比较可做,剩下的暂时没有思路 ...
- [Windows] Prism 8.0 入门(上):Prism.Core
1. Prism 简介 Prism 是一个用于构建松耦合.可维护和可测试的 XAML 应用的框架,它支持所有还活着的基于 XAML 的平台,包括 WPF.Xamarin Forms.WinUI 和 U ...
- moviepy音视频剪辑:使用fl_time进行时间特效处理报错OSError: Error in file xxxx, Accessing time
☞ ░ 前往老猿Python博文目录 ░ 老猿在使用moviepy音视频剪辑的fl_time进行时间特效处理时,系统报错: OSError: Error in file F:\video\WinBas ...
- 第9.11节 Python中IO模块文件打开读写操作实例
为了对前面学习的内容进行一个系统化的应用,老猿写了一个程序来进行文件相关操作功能的测试. 一. 测试程序说明 该程序允许测试人员选择一个文件,自己输入文件打开模式.写入文件的位置以及写入内容,程序按照 ...
- 第14.18节 爬虫实战4: request+BeautifulSoup+os实现利用公众服务Wi-Fi作为公网IP动态地址池
写在前面:本文相关方法为作者独创,仅供参考学习爬虫技术使用,请勿用作它途,禁止转载! 一. 引言 在爬虫爬取网页时,有时候希望不同的时候能以不同公网地址去爬取相关的内容,去网上购买地址资源池是大部分人 ...
- C# Email 帮助类 EmailHelper
1. 配置文件 App.config <?xml version="1.0" encoding="utf-8" ?> <configurati ...
- WordCount个人项目
1.GitHub地址:https://github.com/lyh27/WordCount 2.题目描述 Word Count1. 实现一个简单而完整的软件工具(源程序特征统计程序).2. 进行单元测 ...
- java中==和equals的不同使用方法
System.out.println("input a charact a "); Scanner input2 = new Scanner(System.in); St ...