当前位置:网站首页>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 - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- ADS simulation design of class AB RF power amplifier
- On the problem of reference assignment to reference
- CV learning notes - reasoning and training
- CV learning notes - camera model (Euclidean transformation and affine transformation)
- 4G module at command communication package interface designed by charging pile
- Opencv notes 20 PCA
- Timer and counter of 51 single chip microcomputer
- Emballage automatique et déballage compris? Quel est le principe?
- Opencv image rotation
猜你喜欢

LeetCode - 5 最长回文子串

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

Opencv feature extraction sift

Opencv notes 17 template matching

CV learning notes - reasoning and training

I think all friends should know that the basic law of learning is: from easy to difficult

Swing transformer details-1

yocto 技術分享第四期:自定義增加軟件包支持

Basic use and actual combat sharing of crash tool
随机推荐
Window maximum and minimum settings
Design of charging pile mqtt transplantation based on 4G EC20 module
Vscode markdown export PDF error
2021-10-27
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
El table X-axis direction (horizontal) scroll bar slides to the right by default
When the reference is assigned to auto
LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)
Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
Development of intelligent charging pile (I): overview of the overall design of the system
01仿B站项目业务架构
Basic use and actual combat sharing of crash tool
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
Pymssql controls SQL for Chinese queries
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Dynamic layout management
LeetCode - 919. 完全二叉树插入器 (数组)
Simulate mouse click
CV learning notes ransca & image similarity comparison hash