[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 ...
随机推荐
- day5(图片验证码接口)
1.django缓存设置 django的六种缓存(mysql+redis) :https://www.cnblogs.com/xiaonq/p/7978402.html#i6 1.1安装Django缓 ...
- centos7中安装redis出现的问题
重现步骤: 1.解压redis包后 执行make命令.出现一堆东西,其中有gcc:命令未找到. 解决:安装 yum install gcc-c++(需要有网) 2.安装完gcc命令后,再make.出现 ...
- Python中sorted(iterable, /, *, key=None, reverse=False)的参数中的斜杆是什么意思?
通过help(sorted)查看sorted的帮助文档,显示如下: Help on built-in function sorted in module builtins: sorted(iterab ...
- SQLMap使用指北
简介 sqlmap是一个开源的渗透测试工具,可以用来进行自动化检测,利用SQL注入漏洞,获取数据库服务器的权限.它具有功能强大的检测引擎,针对各种不同类型数据库的渗透测试的功能选项,包括获取数据库中存 ...
- Python Flask后端异步处理(三)
前一篇博文我们已经将基础知识和环境配置进行了介绍:https://www.cnblogs.com/Cl0ud/p/13192925.html,本篇博文在实际应用场景中使用Celery,对Flask后端 ...
- pandas 标签映射成数值的几种方法
1. preprocessing.LabelEncoder() import pandas as pd from sklearn import preprocessing le = preproces ...
- 虚拟IP原理及使用
一.前言 高可用性 HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA 系统是目前企业防止核心 ...
- 团队作业4-Day7
团队作业4-Day7 项目git地址 1. 站立式会议 2. 项目燃尽图 3. 适当的项目截图 4. 代码/文档签入记录(部分) 5. 每人每日总结 吴梓华:今日补充界面小漏洞,修复部分bug 白军强 ...
- AcWing 345. 牛站 Cow Relays
由于我太菜了,不会矩阵乘法,所以给同样不会矩阵乘法同学的福利 首先发现这题点很多边很少,实际上有用的点 \(<= 2 * T\)(因为每条边会触及两个点嘛) 所以我们可以把点的范围缩到 \(2 ...
- CSP-S2020 DP专项训练
前言 \(\text{CPS-S2020}\) 已然临近,而 \(\text{DP}\) 作为联赛中的常考内容,是必不可少的复习要点,现根据教练和个人刷题,整理部分好题如下(其实基本上是直接搬--). ...