LeetCode 12. 整数转罗马数字

思路

进制打表,对应大小组成罗马数字。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
string intToRoman(int num) {
int n=num;
vector<int> bits={1000,900,500,400,100,90,50,40,10,9,5,4,1};
vector<string> Louma={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string res="";
while(n>0){
int i=0;
while(n<bits[i]){
i++;
}
res+=Louma[i];
n-=bits[i];
}
return res;
}
};

[^1]: