题目

12. 整数转罗马数字

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
string intToRoman(int num) {
int thousand = num/1000;
int hundred = num%1000/100;
int ten = num%100/10;
int one = num%10;
//枚举每位数的所有可能情况,并且按从小到大进行排序
string thousands[] = {"", "M", "MM", "MMM"};
string hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return thousands[thousand]+hundreds[hundred]+tens[ten]+ones[one];
}
};

__END__