Question: Integer to Roman Numerals
Question solution: The range is limited to 0-3999, hard-coded is good–too lazy to think about logic
class Solution {
public:
string intToRoman(int num) {
int a = num / 1000;
int b = (num / 100)% < span style="color: #800080;">10;
int c = (num / 10)% < span style="color: #800080;">10;
int d = num% 10;
string ans;
for (int i = 0; i "M";
if (b == 9)ans = ans + "CM" span>;
else if (b == 8)ans = ans + "DCCC";
else if (b == 7)ans = ans + "DCC span>";
else if (b == 6)ans = ans + "DC";
else if (b == 5)ans = ans + "D span>";
else if (b == 4)ans = ans + "CD span>";
else if (b == 3)ans = ans + "CCC";
else if (b == 2)ans = ans + "CC";
else if (b == 1)ans = ans + "C";
if (c == 9)ans = ans + "XC" span>;
else if (c == 8)ans = ans + "LXXX span>";
else if (c == 7)ans = ans + "LXX span>";
else if (c == 6)ans = ans + "LX";
else if (c == 5)ans = ans + "L";
else if (c == 4)ans = ans + "XL span>";
else if (c == 3)ans = ans + "XXX span>";
else if (c == 2)ans = ans + "XX span>";
else if (c == 1)ans = ans + "X span>";
if (d == 9)ans = ans + "IX" span>;
else if (d == 8)ans = ans + "VIII span>";
else if (d == 7)ans = ans + "VII";
else if (d == 6)ans = ans + "VI span>";
else if (d == 5)ans = ans + "V span>";
else if (d == 4)ans = ans + "IV span>";
else if (d == 3)ans = ans + "III span>";
else if (d == 2)ans = ans + "II span>";
else if (d == 1)ans = ans + "I span>";
return ans;
}
};
class Solution {
public:
string intToRoman(int num) {
int a = num / 1000;
int b = (num / 100)% < span style="color: #800080;">10;
int c = (num / 10)% < span style="color: #800080;">10;
int d = num% 10;
string ans;
for (int i = 0; i "M";
if (b == 9)ans = ans + "CM" span>;
else if (b == 8)ans = ans + "DCCC";
else if (b == 7)ans = ans + "DCC span>";
else if (b == 6)ans = ans + "DC";
else if (b == 5)ans = ans + "D span>";
else if (b == 4)ans = ans + "CD span>";
else if (b == 3)ans = ans + "CCC";
else if (b == 2)ans = ans + "CC";
else if (b == 1)ans = ans + "C";
if (c == 9)ans = ans + "XC" span>;
else if (c == 8)ans = ans + "LXXX span>";
else if (c == 7)ans = ans + "LXX span>";
else if (c == 6)ans = ans + "LX";
else if (c == 5)ans = ans + "L";
else if (c == 4)ans = ans + "XL span>";
else if (c == 3)ans = ans + "XXX span>";
else if (c == 2)ans = ans + "XX span>";
else if (c == 1)ans = ans + "X span>";
if (d == 9)ans = ans + "IX" span>;
else if (d == 8)ans = ans + "VIII span>";
else if (d == 7)ans = ans + "VII";
else if (d == 6)ans = ans + "VI span>";
else if (d == 5)ans = ans + "V span>";
else if (d == 4)ans = ans + "IV span>";
else if (d == 3)ans = ans + "III span>";
else if (d == 2)ans = ans + "II span>";
else if (d == 1)ans = ans + "I span>";
return ans;
}
};