012. Integer to Roman
给于一个整型数字范围在1-3999,将它转化为罗马数字
题目
思路1
分类讨论,暴力解决。
1 | class Solution { |
写完之后都有点怀疑自己。。。
略微洋气点的
把10个数字直接用罗马数字写出来1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25class Solution
{
public:
string intToRoman(int num)
{
string one[11] = {"", "I","II","III","IV","V","VI","VII","VIII","IX"}; /// 个位
string ten[11] = {"", "X", "XX","XXX","XL","L","LX","LXX","LXXX","XC"}; /// 十位
string hundred[11] = {"", "C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; /// 百位
string thousand[4] = {"", "M", "MM","MMM"}; /// 千位
string result = "";
string* trans[4] = {one, ten, hundred, thousand};
int index = 0;
while (num > 0)
{
result = trans[index][num % 10] + result;
num = num / 10;
index++;
}
return result;
}
};
其实也不算多么洋气的解法。。。
012. Integer to Roman