当前位置:网站首页>Fraction to recursing decimal
Fraction to recursing decimal
2022-06-23 06:02:00 【ruochen】
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)".
- First judge the symbol , Use Math.signum(). If it's a positive number , return 1; negative , return -1; yes 0, return 0.
- Considering spillover , Auxiliary variables are defined as long. This is because if : So in numerator * 10 after , It overflowed .
- To take the absolute value , because -2 % 3 = -2, -2 % -3 = -2
- analysis 57, The remainder is respectively 5 1 3 2 6 4 5, To 5 There is a cycle at . Because the remainder must be [0,7) Within the scope of , If you can't get rid of , It must be an infinite circular decimal . The position of the loop , Is the position where a remainder first appears , To the current position where the remainder appears ( The remainder is one of the remainder , The first to appear 2 Time of ).
public String fractionToDecimal(int numerator, int denominator) {
String sign = "";
if (Math.signum(numerator) * Math.signum(denominator) < 0) {
sign = "-";
}
long n = Math.abs(numerator);
long d = Math.abs(denominator);
String intPart = Math.abs(n / d) + "";
// If you divide it , Direct return
if (n % d == 0) {
return sign + intPart;
}
// Calculate the decimal part
n %= d;
n *= 10;
StringBuilder sb = new StringBuilder();
Map<Long, Integer> mod = new HashMap<Long, Integer>();
for (int i = 0; n != 0; i++) {
long q = n / d;
Integer start = mod.get(n / 10);
if (start != null) {
sb.insert(start, "(");
sb.append(")");
break;
}
sb.append(Math.abs(q));
mod.put(n / 10, i);
n %= d;
n *= 10;
}
String fractionalPart = sb.toString();
return sign + intPart + "." + fractionalPart;
}边栏推荐
- Basic calculator II for leetcode topic analysis
- True MySQL interview question (XXII) -- condition screening and grouping screening after table connection
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-7 personal solutions
- jvm-05.垃圾回收
- 最优传输理论下对抗攻击可解释性
- Leetcode topic analysis: factorial training zeroes
- Genetic engineering of AI art? Use # artbreeder to change any shape of the image
- Kotlin Android simple activity jump, simple combination of handler and thread
- PAT 乙等 1022 D进制的A+B
- PAT 乙等 1014 C语言
猜你喜欢

最优传输理论下对抗攻击可解释性

jvm-06. Garbage collector

Genetic engineering of AI art? Use # artbreeder to change any shape of the image

How can digital collections empower economic entities?

The construction of digital factory can be divided into three aspects

Use of visdom

MDM data cleaning function development description

Android handler memory leak kotlin memory leak handling

True MySQL interview question (21) - Finance - overdue loan

Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
随机推荐
jvm-02. Guarantee of orderliness
matplotlib savefig多个图片叠加问题
Basic calculator for leetcode topic analysis
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
PAT 乙等 1013 C语言
jvm-03. JVM memory model
Operating mongodb in node
Kotlin Android simple activity jump, simple combination of handler and thread
What is the magic of digital collections? Which reliable teams are currently developing
Heimdall database proxy scale out 20 times
Pat class B 1022 d-ary a+b
Prometheus, incluxdb2.2 installation and flume_ Export download compile use
技术开发团队视角看到的数字藏品机遇与挑战
Pat class B 1017 C language
Memory analysis and memory leak detection
exe闪退的原因查找方法
【开源项目】excel导出lua配置表工具
The hierarchyviewer tool cannot find the hierarchyviewer location
JS interview question - anti shake function
Pat class B 1013 C language