Skip to content

Commit c453c16

Browse files
committed
012.Integer_to_Roman
1 parent 54683d6 commit c453c16

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_simulation_1.java
4+
* Create Date: 2015-03-02 23:06:05
5+
* Descripton:
6+
*/
7+
8+
import java.util.Scanner;
9+
10+
public class Solution {
11+
private int[] val = {
12+
1000, 900, 500, 400,
13+
100, 90, 50, 40,
14+
10, 9, 5, 4,
15+
1
16+
};
17+
private String[] syb = new String[] {
18+
"M", "CM", "D", "CD",
19+
"C", "XC", "L", "XL",
20+
"X", "IX", "V", "IV",
21+
"I"
22+
};
23+
24+
25+
public String intToRoman(int num) {
26+
StringBuilder roman = new StringBuilder();
27+
int i = 0, k;
28+
while (num > 0) {
29+
k = num / val[i];
30+
while (k-- > 0) {
31+
roman.append(syb[i]);
32+
num -= val[i];
33+
}
34+
i++;
35+
}
36+
return roman.toString();
37+
}
38+
39+
// debug
40+
public static void main(String[] args) {
41+
Scanner cin = new Scanner(System.in);
42+
Solution s = new Solution();
43+
int[] input = {1, 2, 3, 1};
44+
System.out.println("no case");
45+
}
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Author: illuz <iilluzen[at]gmail.com>
4+
# File: AC_simulation_1.py
5+
# Create Date: 2015-03-02 23:02:51
6+
# Usage: AC_simulation_1.py
7+
# Descripton:
8+
9+
10+
class Solution:
11+
# @return a string
12+
def intToRoman(self, num):
13+
val = [
14+
1000, 900, 500, 400,
15+
100, 90, 50, 40,
16+
10, 9, 5, 4,
17+
1
18+
]
19+
syb = [
20+
"M", "CM", "D", "CD",
21+
"C", "XC", "L", "XL",
22+
"X", "IX", "V", "IV",
23+
"I"
24+
]
25+
roman = ''
26+
i = 0
27+
while num > 0:
28+
for _ in range(num // val[i]):
29+
roman += syb[i]
30+
num -= val[i]
31+
i += 1
32+
return roman
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 012.Integer_to_Roman (Medium)
2+
3+
### **链接**
4+
题目:https://oj.leetcode.com/problems/integer-to-roman/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
把十进制转为罗马数。
9+
10+
### **分析**
11+
模拟即可。
12+
13+
“罗马数字的基本符号有I(表示十进制数1),V(表示5),X(表示10),L(表示50),C(表示100),D(表示500),M(表示1000)。” -- [罗马数制(百度百科)](http://baike.baidu.com/view/1246899.htm)
14+
15+

0 commit comments

Comments
 (0)