当前位置:网站首页>20220606 Mathematics: fraction to decimal
20220606 Mathematics: fraction to decimal
2022-07-03 10:11:00 【Seeyouagain】
Title Description : Given two integers , Molecules representing fractions respectively numerator Denominator denominator, With Returns a decimal as a string . If the decimal part is a circular decimal , The loop is enclosed in brackets . If there are multiple answers , Just go back Any one . For all given inputs , Guarantee The length of the answer string is less than 10^4 .
coded :
public String fractionToDecimal(int numerator, int denominator) {
if (denominator == 0) {
return "";
}
if (numerator == 0) {
return "0";
}
StringBuilder result = new StringBuilder();
long num = numerator,den = denominator;
if ((num >= 0) ^ (den >= 0)) {
result.append("-");
}
num = Math.abs(num);
den = Math.abs(den);
result.append(num / den);
num %= den;
if (num == 0) {
return result.toString();
}
result.append(".");
int index = result.length() - 1;
Map<Long, Integer> record = new HashMap<>();
while (num != 0 && !record.containsKey(num)) {
record.put(num, ++index);
num *= 10;
result.append(num / den);
num %= den;
}
if (record.containsKey(num)) {
result.insert(record.get(num), "(");
result.append(")");
}
return result.toString();
}
边栏推荐
- Vgg16 migration learning source code
- 20220603数学:Pow(x,n)
- CV learning notes - clustering
- 20220602数学:Excel表列序号
- CV learning notes - scale invariant feature transformation (SIFT)
- Leetcode bit operation
- Leetcode-112: path sum
- Dynamic layout management
- Installation and removal of MySQL under Windows
- Opencv feature extraction sift
猜你喜欢

QT is a method of batch modifying the style of a certain type of control after naming the control

CV learning notes - reasoning and training

Opencv image rotation

Flutter 退出当前操作二次确认怎么做才更优雅?

Dictionary tree prefix tree trie

LeetCode - 705 设计哈希集合(设计)

Opencv gray histogram, histogram specification

Discrete-event system

Leetcode-513: find the lower left corner value of the tree

Yocto Technology Sharing Phase 4: Custom add package support
随机推荐
Serial port programming
2021-10-28
LeetCode - 5 最长回文子串
On the problem of reference assignment to reference
Vscode markdown export PDF error
01 business structure of imitation station B project
My notes on intelligent charging pile development (II): overview of system hardware circuit design
Label Semantic Aware Pre-training for Few-shot Text Classification
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
【C 题集】of Ⅵ
Leetcode bit operation
LeetCode - 919. Full binary tree inserter (array)
Opencv notes 17 template matching
Tensorflow2.0 save model
Leetcode-100:相同的树
Tensorflow built-in evaluation
El table X-axis direction (horizontal) scroll bar slides to the right by default
Yocto technology sharing phase IV: customize and add software package support
Development of intelligent charging pile (I): overview of the overall design of the system
Toolbutton property settings