feat: 166_fraction_to_recurring_decimal
This commit is contained in:
parent
c7457e738a
commit
2a7b3b2690
7
166_fraction_to_recurring_decimal/Cargo.lock
generated
Normal file
7
166_fraction_to_recurring_decimal/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "fraction_to_recurring_decimal"
|
||||
version = "0.1.0"
|
6
166_fraction_to_recurring_decimal/Cargo.toml
Normal file
6
166_fraction_to_recurring_decimal/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "fraction_to_recurring_decimal"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
61
166_fraction_to_recurring_decimal/src/lib.rs
Normal file
61
166_fraction_to_recurring_decimal/src/lib.rs
Normal file
@ -0,0 +1,61 @@
|
||||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
|
||||
if numerator == 0 {
|
||||
return "0".to_string();
|
||||
}
|
||||
|
||||
let mut result = String::new();
|
||||
|
||||
if numerator.is_negative() != denominator.is_negative() {
|
||||
result.push('-');
|
||||
}
|
||||
let mut numerator = numerator.unsigned_abs() as u64;
|
||||
let mut denominator = denominator.unsigned_abs() as u64;
|
||||
|
||||
let gcd = Solution::gcd(numerator, denominator);
|
||||
numerator /= gcd;
|
||||
denominator /= gcd;
|
||||
|
||||
result.push_str(&(numerator / denominator).to_string());
|
||||
if numerator % denominator == 0 {
|
||||
return result;
|
||||
}
|
||||
|
||||
result.push('.');
|
||||
|
||||
let mut remains = std::collections::HashMap::<u64, usize>::new();
|
||||
loop {
|
||||
remains.insert(numerator % denominator, result.len());
|
||||
numerator %= denominator;
|
||||
numerator *= 10;
|
||||
result.push(((numerator / denominator) as u8 + '0' as u8) as char);
|
||||
|
||||
if result.len() >= 10000 {
|
||||
break;
|
||||
}
|
||||
|
||||
let remain = numerator % denominator;
|
||||
if remain == 0 {
|
||||
break;
|
||||
} else if let Some(index) = remains.get(&remain) {
|
||||
result.insert(*index, '(');
|
||||
result.push(')');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn gcd(a: u64, b: u64) -> u64 {
|
||||
if a == 0 {
|
||||
b
|
||||
} else if a <= b {
|
||||
Solution::gcd(b % a, a)
|
||||
} else {
|
||||
Solution::gcd(b, a)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user