Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
有了前面Roman to Integer的基础,本来想仿照着做一下,不过还是很复杂的样子……
网上查到的说是要用到贪心算法,这个以后自己还要仔细学,这里就简单看一看吧
class Solution {
public:
string intToRoman(int num) {
string roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string r;
int n[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
int i;
for(i = 0; num != 0; i++) {
while(num >= n[i]) {
num -= n[i];
r += roman[i];
}
}
return r;
}
};