当前位置:网站首页>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;
}边栏推荐
- Pat class B 1018 C language
- PAT 乙等 1009 C语言
- Behind the hot digital collections, a strong technical team is needed to support the northern technical team
- jvm-06.垃圾回收器
- jvm-01.指令重排
- Pat class B 1010 C language
- Digital collections - new investment opportunities
- Pat class B 1012 C language
- jvm-01. Instruction rearrangement
- 内存分析与内存泄漏检测
猜你喜欢

jvm-01.指令重排

Deploy docker and install MySQL in centos7

Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method

Real MySQL interview question (23) -- pinduoduo ball game analysis

The digital collection market has just begun

数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发

The performance of nonstandard sprintf code in different platforms

Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter

jvm-03. JVM memory model

Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
随机推荐
The digital collection market has just begun
PAT 乙等 1022 D进制的A+B
Raspberry pie assert preliminary exercise
[open source project] excel export Lua configuration table tool
Memory analysis and memory leak detection
The performance of nonstandard sprintf code in different platforms
PAT 乙等 1025 反转链表
What is the magic of digital collections? Which reliable teams are currently developing
Deploy docker and install MySQL in centos7
Pat class B 1022 d-ary a+b
New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!
Summary of ant usage (I): using ant to automatically package apk
PAT 乙等 1026 程序运行时间
PAT 乙等 1014 C语言
线性表 链表结构的实现
Pat class B 1012 C language
A bit of knowledge - folding forging and Damascus steel
PAT 乙等 1020.月饼
How can digital collections empower economic entities?
Activity startup mode and life cycle measurement results