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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#![allow(dead_code)]

pub fn int_to_roman(num: i32) -> String {
    let mut res = String::from("");
    let mut num = num;
    while num >= 1000 {
        res.push('M');
        num -= 1000;
    }
    if num >= 900 {
        res.push_str("CM");
        num -= 900;
    }
    if num >= 500 {
        res.push('D');
        num -= 500;
    }
    if num >= 400 {
        res.push_str("CD");
        num -= 400;
    }
    while num >= 100 {
        res.push('C');
        num -= 100;
    }
    if num >= 90 {
        res.push_str("XC");
        num -= 90;
    }
    if num >= 50 {
        res.push('L');
        num -= 50;
    }
    if num >= 40 {
        res.push_str("XL");
        num -= 40;
    }
    while num >= 10 {
        res.push('X');
        num -= 10;
    }
    if num >= 9 {
        res.push_str("IX");
        num -= 90;
    }
    if num >= 5 {
        res.push('V');
        num -= 5;
    }
    if num >= 4 {
        res.push_str("IV");
        num -= 4;
    }
    while num >= 1 {
        res.push('I');
        num -= 1;
    }

    res
}

pub fn int_to_roman2(num: i32) -> String {
    let romans = vec![
        "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I",
    ];
    let integers = vec![1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    let mut res = String::from("");
    let mut num = num;
    for (i, &val) in integers.iter().enumerate() {
        while num >= val {
            num -= val;
            res.push_str(romans[i])
        }
    }

    res
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test1() {
        assert_eq!(int_to_roman(1994), String::from("MCMXCIV"));
    }

    #[test]
    fn test2() {
        assert_eq!(int_to_roman2(1994), String::from("MCMXCIV"));
    }
}