当前位置:网站首页>20220606数学:分数到小数
20220606数学:分数到小数
2022-07-03 09:20:00 【丿SeeYouAgain】
题目描述:给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数 。如果小数部分为循环小数,则将循环的部分括在括号内。如果存在多个答案,只需返回 任意一个 。对于所有给定的输入,保证 答案字符串的长度小于 10^4 。
编码实现:
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();
}
边栏推荐
- LeetCode - 673. Number of longest increasing subsequences
- YOLO_ V1 summary
- When the reference is assigned to auto
- LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
- 20220609其他:多数元素
- Leetcode-112:路径总和
- 2.2 DP: Value Iteration & Gambler‘s Problem
- ADS simulation design of class AB RF power amplifier
- [combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
- 20220604数学:x的平方根
猜你喜欢

CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)

yocto 技术分享第四期:自定义增加软件包支持

Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*

Interruption system of 51 single chip microcomputer

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

LeetCode - 5 最长回文子串

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

LeetCode - 919. Full binary tree inserter (array)

Opencv note 21 frequency domain filtering

Adaptiveavgpool1d internal implementation
随机推荐
CV learning notes - image filter
Opencv histogram equalization
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
Vgg16 migration learning source code
03 fastjason solves circular references
When the reference is assigned to auto
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
(1) What is a lambda expression
Leetcode-112:路径总和
Opencv feature extraction sift
Markdown latex full quantifier and existential quantifier (for all, existential)
Modelcheckpoint auto save model
2.1 Dynamic programming and case study: Jack‘s car rental
4G module at command communication package interface designed by charging pile
Opencv notes 17 template matching
(1) 什么是Lambda表达式
Opencv image rotation
Leetcode 300 longest ascending subsequence
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
Wireshark use