-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintToRoman.java
62 lines (53 loc) · 1.26 KB
/
intToRoman.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
Given an integer, convert it to a roman numeral, and return a string corresponding to its roman numeral version
Input is guaranteed to be within the range from 1 to 3999.
Example :
Input : 5
Return : "V"
Input : 14
Return : "XIV"
*/
public String intToRoman(int a) {
if(a==0)
return "";
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "I");
map.put(4, "IV");
map.put(5, "V");
map.put(9, "IX");
map.put(10, "X");
map.put(40, "XL");
map.put(50, "L");
map.put(90, "XC");
map.put(100, "C");
map.put(400, "CD");
map.put(500, "D");
map.put(900, "CM");
map.put(1000, "M");
String re = "";
int[] bases = { 1000,
900,
500,
400,
100,
90,
50,
40,
10,
9,
5,
4,
1};
int i =0;
while(i<bases.length)
{
if(a>0 && a>=bases[i])
{
re+=map.get(bases[i]);
a-=bases[i];
}
else
i++;
}
return re;
}