这道题写的,思路清奇?代码太臃肿了
也算是有一丢丢特点,拿出来分享一下
class Solution {
public:
string intToRoman(int num)
{
stack<char> temp;
int count = 0;
while (num)
{
++count;
int r = num % 10;
num /= 10;
if (count == 1)
{
if (r <= 3)
{
int k = r;
while (k--) temp.push('I');
}
if (r == 4)
{
temp.push('V');
temp.push('I');
}
if (r == 5) temp.push('V');
if (r>5 && r <= 8)
{
int res = r - 5;
while (res--)
{
temp.push('I');
}
temp.push('V');
}
if (r == 9)
{
temp.push('X');
temp.push('I');
}
}
if (count == 2)
{
if (r <= 3)
{
int k = r;
while (k--) temp.push('X');
}
if (r == 4)
{
temp.push('L');
temp.push('X');
}
if (r == 5) temp.push('L');
if (r>5 && r <= 8)
{
int res = r-5;
while (res--)
{
temp.push('X');
}
temp.push('L');
}
if (r == 9)
{
temp.push('C');
temp.push('X');
}
}
if (count == 3)
{
if (r <= 3)
{
int k = r;
while (k--) temp.push('C');
}
if (r == 4)
{
temp.push('D');
temp.push('C');
}
if (r == 5) temp.push('D');
if (r>5 && r <= 8)
{
int res = r - 5;
while (res--)
{
temp.push('C');
}
temp.push('D');
}
if (r == 9)
{
temp.push('M');
temp.push('C');
}
}
if (count == 4)
{
int k = r;
while (k--) temp.push('M');
}
}
string str = "";
while (!temp.empty())
{
str += temp.top();
temp.pop();
}
return str;
}
};