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

不过也学到了:

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. iOS UIImageView contentMode使用详解

    UIImageView 的contentMode这个属性是用来设置图片的显示方式,如居中.居右,是否缩放等,有以下几个常量可供设定:UIViewContentModeScaleToFill //填满i ...

  2. java46

    1.迭代器遍历 import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public c ...

  3. Docker实战 | 第三篇:Docker安装Nginx,实现基于vue-element-admin框架构建的项目线上部署

    一. 前言 在上一文中 点击跳转 通过IDEA集成Docker插件实现微服务的一键部署,但 youlai-mall 是前后端分离的项目,除了后端微服务的部署之外,当然还少不了前端工程的部署.所以本篇讲 ...

  4. 第四十章、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...

  5. 第15.4节 PyCharm程序代码检测功能介绍

    老猿使用PyCharm有将近一个月了,发现PyCharm并不能很好的完成语法检查,有时运行时突然终止,仔细核查却发现是基本的语法错误,不过有次无意中移动鼠标到代码最右边的边框时发现其实PyCharm有 ...

  6. 公司只提供签名服务,不提供证书文件,如何打包Electron应用

    需求 稍微正规点的公司,都要为自己开发的软件做代码签名,如下图所示 代码签名的主要目的是为了确保软件的来源(这个软件是由谁生产的)和软件的内容不被篡改 一个软件公司可能有很多团队,很多开发者,开发不同 ...

  7. Codeforces Edu Round 52 A-E

    A. Vasya and Chocolate 模拟题.数据会爆\(int\),要开\(long\) \(long\) #include <iostream> #include <cs ...

  8. 《图解TCP/IP》笔记

    OSI参考模型 协议分层 为什么需要分层? 简化网络协议. 每一层只需要衔接上下层的服务. 利于模块化开发. 解耦. 分层的问题 过分模块化.提高数据处理的开销. OSI参考模型 作用及意义 将复杂的 ...

  9. nginx学习之——信号控制和配置

    一.信号控制 1)TERM, INT   Quick shutdown  \\麻溜停掉(暴力停止),一般不常用 // 启动和停止nginx 当前目录:/usr/local/bin/nginx 启动: ...

  10. 大白话详解大数据hive知识点,老刘真的很用心(2)

    前言:老刘不敢说写的有多好,但敢保证尽量用大白话把自己复习的内容详细解释出来,拒绝资料上的生搬硬套,做到有自己的了解! 1. hive知识点(2) 第12点:hive分桶表 hive知识点主要偏实践, ...