-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution_integertoRoman.java
167 lines (132 loc) · 3.23 KB
/
Solution_integertoRoman.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.bupt.kcrosswind.Leetcode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Solution_integertoRoman {
public static class NumeralInvoke {
int number;
String numerbal;
NumeralInvoke(int x, String s) {
number = x;
numerbal = s;
}
}
public static List<Integer> getDigit(int x) {// 拆分成单独的数字
List<Integer> arr = new ArrayList();
int flag = 1;
long dividend0;
long dividend1;
long temp = x;
while (true) {
dividend0 = (long) Math.pow(10, flag);
dividend1 = (long) Math.pow(10, flag - 1);
// arr.add((int)x % dividend0 / dividend1);
// 这样写不对的!!结果存在一个空间这个空间无法转换格式,下面的就不一样,他有新的空间赋予给转换的变量!要看看java编程思想
long digit = x % dividend0 / dividend1;
arr.add((int) digit);
flag++;
if (x % dividend0 == x) {
break;
}
}
System.out.println(arr);
return arr;
}
public static String intToRoman(int num) {// roman 基本可以确定由右向左
String reslut = new String();
List<Integer> digits = new ArrayList<Integer>();
digits = getDigit(num);
int size = digits.size();
while (size > 0) {
int temp = digits.get(size - 1);
if (size == 4) {
while (temp > 0) {
reslut = reslut + "M";
temp--;
}
} else if (size == 3) {
if (temp < 4) {
int temp2 = temp;
while (temp2 > 0) {
reslut = reslut + "C";
temp2--;
}
} else if (temp == 4) {
reslut = reslut + "CD";
} else if (temp == 5) {
reslut = reslut + "D";
} else if (temp == 9) {
reslut = reslut + "CM";
} else {
reslut = reslut + "D";
int temp2 = temp;
while (temp2 > 5) {
reslut = reslut + "C";
temp2--;
}
}
} else if (size == 2) {
if (temp < 4) {
int temp2 = temp;
while (temp2 > 0) {
reslut = reslut + "X";
temp2--;
}
} else if (temp == 4) {
reslut = reslut + "XL";
} else if (temp == 5) {
reslut = reslut + "L";
} else if (temp == 9) {
reslut = reslut + "XC";
} else {
int temp2 = temp;
reslut = reslut + "L";
while (temp2 > 5) {
reslut = reslut + "X";
temp2--;
}
}
} else if (size == 1) {
if (temp < 4) {
int temp2 = temp;
while (temp2 > 0) {
reslut = reslut + "I";
temp2--;
}
} else if (temp == 4) {
reslut = reslut + "IV";
} else if (temp == 5) {
reslut = reslut + "V";
} else if (temp == 9) {
reslut = reslut + "IX";
} else {
reslut = reslut + "V";
int temp2 = temp;
while (temp2 > 5) {
reslut = reslut + "I";
temp2--;
}
}
}
size--;
}
if (num % 10 > 0) {
int remark = num / 1000;
}
return reslut;
}
private static List<NumeralInvoke> getNumeralBase() {
List<NumeralInvoke> numeral = new ArrayList();
numeral.add(new NumeralInvoke(1, "I"));
numeral.add(new NumeralInvoke(5, "V"));
numeral.add(new NumeralInvoke(10, "X"));
numeral.add(new NumeralInvoke(50, "L"));
numeral.add(new NumeralInvoke(100, "C"));
numeral.add(new NumeralInvoke(500, "D"));
numeral.add(new NumeralInvoke(1000, "M"));
return numeral;
}
public static void main(String srgs[]) {
System.out.println(intToRoman(2000));
}
}