-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfraction-to-recurring-decimal.rs
76 lines (70 loc) · 2.68 KB
/
fraction-to-recurring-decimal.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![allow(dead_code, unused, unused_variables)]
fn main() {
println!("{}", Solution::fraction_to_decimal(1, 2));
println!("{}", Solution::fraction_to_decimal(2, 1));
println!("{}", Solution::fraction_to_decimal(4, 333));
println!("{}", Solution::fraction_to_decimal(1, 5));
println!("{}", Solution::fraction_to_decimal(424231, 5));
println!("{}", Solution::fraction_to_decimal(1000000, 3));
println!("{}", Solution::fraction_to_decimal(1, 3));
println!("{}", Solution::fraction_to_decimal(1, 6));
println!("{}", Solution::fraction_to_decimal(1, 333));
println!("{}", Solution::fraction_to_decimal(1, 17));
println!("{}", Solution::fraction_to_decimal(-50, 8));
println!("{}", Solution::fraction_to_decimal(-50, -8));
println!("{}", Solution::fraction_to_decimal(50, -8));
println!("{}", Solution::fraction_to_decimal(-1, -2147483648));
}
struct Solution;
impl Solution {
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
// 用于保存出现过的余数
let mut m = std::collections::HashMap::<i64, usize>::new();
let (mut numerator, mut denominator) = (numerator as i64, denominator as i64);
let mut flag = false; // false为当前为整数部分,true为小数部分
let mut n = 0;
let (mut r1, mut r2) = (String::new(), String::new()); // r1整数,r2小数
if numerator < 0 && denominator > 0 {
r1.push('-');
numerator *= -1;
} else if numerator > 0 && denominator < 0 {
r1.push('-');
denominator *= -1;
} else if numerator < 0 && denominator < 0 {
denominator *= -1;
numerator *= -1;
}
loop {
if !flag {
r1.push_str(&(numerator / denominator).to_string());
numerator %= denominator;
if numerator < denominator {
flag = true;
numerator *= 10;
}
} else {
n += 1;
if let Some(&s) = m.get(&numerator) {
r2 = format!("{}({})", &r2[..s - 1], &r2[s - 1..]);
break;
}
m.insert(numerator, n);
if numerator > denominator {
r2.push_str(&(numerator / denominator).to_string());
numerator %= denominator;
} else {
r2.push('0');
}
numerator *= 10;
}
if numerator == 0 {
break;
}
}
if r2 != "" {
format!("{}.{}", r1, r2)
} else {
r1
}
}
}