[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 ...
随机推荐
- 03Python网络编程系列之服务端
# 这里边是一个定义了服务端的一系列函数,是Python网络编程这本书第七章的第一个例子.# 这是供后边函数进行调用了,然后我们来进行研究网络的单线程编程,多线程编程.异步网络编程等.# 导入网络编程 ...
- LeetCode 045 Jump Game II
题目要求:Jump Game II Given an array of non-negative integers, you are initially positioned at the first ...
- 【佛山市选2013】JZOJ2020年8月7日T4 排列
[佛山市选2013]JZOJ2020年8月7日T4 排列 题目 描述 一个关于n个元素的排列是指一个从{1, 2, -, n}到{1, 2, -, n}的一一映射的函数.这个排列p的秩是指最小的k,使 ...
- CPU实现原子操作的原理
586之前的CPU, 会通过LOCK锁总线的形式来实现原子操作. 686开始则提供了存储一致性(Cache coherence), 这是多处理的基础, 也是原子操作的基础. 1. 存储的粒度 存储的 ...
- 20200203_windows2012下安装mysql 5.7.29
一. 检查系统版本: 二. 下载mysql, 下载地址: https://dev.mysql.com/downloads/mysql/5.7.html#downloads 三. 解压下载后的压 ...
- 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现
庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介 在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...
- 第15.20节 PyQt(Python+Qt)入门学习:QColumnView的作用及开发中对应Model的使用
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 在Qt Designer的Item Views(Model-based)部件中,Colum ...
- PyQt学习随笔:Model/View中视图数据项编辑变动实时获取变动数据的方法
对于Model/View中视图的数据编辑后怎么能实时获取编辑的数据变动位置和变动情况查阅了一些资料,终于基本弄明白必须重写Model的setData方法才能截获.setData方法是视图中各种角色数据 ...
- Hello TLM
前言 目标 了解TLM程序的基本过程.TLM的英文全称是Transaction Level Modeling,中文翻译为事务级建模.它是在SystemC基础上的一个扩展库. 功能描述 模块A向模块B发 ...
- 原生js获取页面所有的checkbox
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...