We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 358027f commit a10ae75Copy full SHA for a10ae75
12. Integer to Roman.java
@@ -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
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments