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