Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
For example,
- Given numerator = 1, denominator = 2, return "0.5".
- Given numerator = 2, denominator = 1, return "2".
- Given numerator = 2, denominator = 3, return "0.(6)".
import java.util.HashMap; public class Solution { public String fractionToDecimal(int numerator, int denominator) { if (numerator == 0) { return new String("0"); } boolean flag = (numerator > 0) ^ (denominator > 0); long Numerator = Math.abs((long)numerator); long Denominator = Math.abs((long)denominator); StringBuffer res = new StringBuffer(); if (flag == true) { res.append("-"); } res.append((Numerator / Denominator)); Numerator = Numerator % Denominator; if (Numerator == 0) { return res.toString(); } res.append("."); HashMap<Long,Integer> hashMap = new HashMap<>(); for (int i = res.length(); Numerator != 0; i++) { if (hashMap.get(Numerator) != null) { break; } hashMap.put(Numerator, i); Numerator *= 10; res.append(Numerator / Denominator); Numerator = Numerator % Denominator; } if (Numerator == 0) { return res.toString(); } res.insert(hashMap.get(Numerator), "("); res.append(")"); return res.toString(); } }
相关推荐
dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring ...Fraction to Recurring Decimal map long long 正负号 Repeated DNA S
Fraction to Recurring Decimal Factorial Trailing Zeroes Nim Game 模拟 Reverse Integer Palindrome Number Insert Interval Merge Intervals Minimum Window Substring Multiply Strings Substring with ...
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...
字符串可能的余数分数到循环小数 给定两个表示分数分子和分母的整数,以字符串格式返回分数。 如果小数部分是重复的,请将重复部分括在括号中。 Example 1: Input: numerator = ...分子和分母都是负数,应该得到正分
Fraction代码参考Fraction代码参考Fraction代码参考
定义一个分数类(Fraction) 实例变量:分子,分母 方法:初始化方法(2个参数),便利构造器,约分,打印,加,减,乘,除。
fraction的pde版代码
可视化如何将分数写为十进制数字 我编写了一个Web应用程序,当小数(例如¼)以十进制形式(0.25)书写时,该应用程序会生成显示数字序列的图表。 可以对每个分数(非负)使用一个图表,底部带有特定值(分母)。...
简单的C++分数类实现,包含运算符的实现和重载
用c++写了个class fraction
1.自定义分数类fraction,使用该类可以完成分数的输入、分数的加、减、乘、除二目运算和一目减运算、分数的约分操作、分数的倒数运算、对两个分数进行六种比较运算、以及对分数的输出等操作。 2.将其中使用的普通...
Fraction.java
参考_fraction.cpp
this is a traditional book about continued fraction.
Fraction类使用属性的方法,实现了分数的约分,打印,加法,减法,乘法,除法四则运算
分式的计算 赋值 定义 拷贝 重载各种操作符 支持各种运算 gcd 简化
设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。具体细节请参考压缩包中的readme.txt文件!
no individual can understand more than a tiny fraction of it, it is useful to know not just which arrangements of symbols form grammatically correct mathematical statements, but also which of these ...
Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the denominator and print the decimal representation. If the decimal representation has a repeating ...
ClassFraction20211122.cpp