题目:
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
解答:
慢慢模拟也可以。应该不会错。这里介绍一种“查表+ 拼接字符串”的方法。
class Solution { public: string thousand[4] = {"", "M", "MM", "MMM"}; string hundred[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; string tens[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; string ones[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; string intToRoman(int num) { string ans; ans += thousand[(int)(num / 1000) % 10]; ans += hundred[(int)(num / 100) % 10]; ans += tens[(int)(num / 10) % 10]; ans += ones[num % 10]; return ans; } };
【LeetCode从零单刷】Integer to Roman
原文地址:http://blog.csdn.net/ironyoung/article/details/45690301