-
Notifications
You must be signed in to change notification settings - Fork 1
/
12. Integer to Roman.java
48 lines (39 loc) · 1.25 KB
/
12. Integer to Roman.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
class Solution {
public String intToRoman(int num) {
int arr[] = new int[]{1,4,5,9,10,40,50,90,100,400,500,900,1000};
String logs[] = new String[]{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int quo = 0;
String res = "";
while(num!=0)
{
for(int i=arr.length-1;i>=0;i--)
{
if(arr[i]<=num)
{
quo = num /arr[i];
if(quo==1)
{
res = res+logs[i];
num = num - arr[i];
}
else if(quo==2)
{
res = res+logs[i]+logs[i];
num = num - arr[i]-arr[i];
}
else if(quo==3)
{
res = res+logs[i]+logs[i]+logs[i];
num = num - arr[i]-arr[i]-arr[i];
}
else
{
//System.out.println("IMPOSSIBLE");
}
//System.out.println(res);
}
}
}
return res;
}
}