-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanNumeralsEncoder.java
46 lines (35 loc) · 1.16 KB
/
RomanNumeralsEncoder.java
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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.util.TreeMap;
// https://www.codewars.com/kata/51b62bf6a9c58071c600001b
public class RomanNumeralsEncoder {
private static final TreeMap<Integer, String> romanNumerals = new TreeMap<>();
static {
romanNumerals.put(1000, "M");
romanNumerals.put(900, "CM");
romanNumerals.put(500, "D");
romanNumerals.put(400, "CD");
romanNumerals.put(100, "C");
romanNumerals.put(90, "XC");
romanNumerals.put(50, "L");
romanNumerals.put(40, "XL");
romanNumerals.put(10, "X");
romanNumerals.put(9, "IX");
romanNumerals.put(5, "V");
romanNumerals.put(4, "IV");
romanNumerals.put(1, "I");
}
private final int input;
public RomanNumeralsEncoder(int input) {
this.input = input;
}
public String solution() {
return getRomanNumber(input);
}
private String getRomanNumber(int n) {
int value = romanNumerals.floorKey(n);
if (n == value) {
return romanNumerals.get(n);
}
return romanNumerals.get(value) + getRomanNumber(n - value);
}
}