Skip to content

Commit a10ae75

Browse files
committed
12
1 parent 358027f commit a10ae75

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

12. Integer to Roman.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
题目:
3+
12. Integer to Roman
4+
Given an integer, convert it to a roman numeral.
5+
6+
Input is guaranteed to be within the range from 1 to 3999.
7+
8+
*/
9+
10+
/*
11+
解析:
12+
关于这道题,我不怎么喜欢,,,因为我不知道什么是罗马数字啊,,
13+
罗马数字: 共有7个,I(1) V(5) X(10) L(50) C(100) D(500) M(1000)
14+
罗马数字没有0,与进位制无关。
15+
16+
17+
*/
18+
19+
//代码:
20+
//借鉴了一下网站上面的代码
21+
//数组是降序排列
22+
class Solution {
23+
public String intToRoman(int num) {
24+
int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
25+
String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
26+
StringBuffer roman = new StringBuffer();//罗马数字
27+
for(int i = 0;i<values.length;i++) {//遍历整个数组
28+
while(num >=values[i]) {
29+
num -= values[i];
30+
roman.append(strs[i]);
31+
}
32+
}
33+
return roman.toString();
34+
35+
36+
}
37+
}

0 commit comments

Comments
 (0)