当前位置:网站首页>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();
}
边栏推荐
- pycharm 无法引入自定义包
- Deep Reinforcement learning with PyTorch
- Discrete-event system
- Opencv notes 20 PCA
- CV learning notes alexnet
- Vscode markdown export PDF error
- My notes on the development of intelligent charging pile (III): overview of the overall design of the system software
- LeetCode - 5 最长回文子串
- LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
- Leetcode 300 longest ascending subsequence
猜你喜欢

Basic knowledge of communication interface

CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)

LeetCode - 919. Full binary tree inserter (array)

Retinaface: single stage dense face localization in the wild

Cases of OpenCV image enhancement

2.2 DP: Value Iteration & Gambler‘s Problem

Pycharm cannot import custom package

Opencv histogram equalization

Opencv gray histogram, histogram specification

The underlying principle of vector
随机推荐
Positive and negative sample division and architecture understanding in image classification and target detection
01 business structure of imitation station B project
Leetcode-100: same tree
Opencv note 21 frequency domain filtering
STM32 running lantern experiment - library function version
LeetCode - 900. RLE 迭代器
Google browser plug-in recommendation
Stm32 NVIC interrupt priority management
CV learning notes ransca & image similarity comparison hash
YOLO_ V1 summary
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
4G module designed by charging pile obtains signal strength and quality
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
ADS simulation design of class AB RF power amplifier
2021-10-28
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
yocto 技术分享第四期:自定义增加软件包支持
51 MCU tmod and timer configuration
2021-10-27
My notes on the development of intelligent charging pile (III): overview of the overall design of the system software