Title Description
Given an integer, convert it to a Roman numeral. To ensure that the input is in the range of 1 to 3999.
Thinking
First, the digital conversion of strings, one by one process, the function call can help
Code
class Solution {
public:
string Help(string s, int i, int len)
{
string record = "IVXLCDMT";
string res;
int number = s[i] - '0';
if (number>0)
{
int a = number / 5;
int b = number % 5;
if (b == 4)
{
res += record[(len - i) * 2 - 2];
res += record[(len - i) * 2 - 1 + a];
}
else
{
if (a == 1)
res += record[(len - i) * 2 - 1];
while (b > 0)
{
res += record[(len - i) * 2 - 2];
b--;
}
}
}
return res;
}
string intToRoman(int num) {
string res;
string s = to_string(num);
int len = s.length();
for (int i = 0; i< len; i++)
{
res += Help(s, i, len);
}
return res;
}
};