leetcode: Integer to Roman

http://oj.leetcode.com/problems/integer-to-roman/

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

思路

先google一下罗马数字表示法。然后查表搞定。

 1 static char* roman_table[4][9] = {{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, // 1 - 9
 2     {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, // 10 - 90
 3     {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, // 100 - 900
 4     {"M", "MM", "MMM"}}; // 1000 - 3000
 5                             
 6 class Solution {
 7 public:
 8     string intToRoman(int num) {
 9         string result;
10         int level = 0;
11         
12         while (num > 0) {
13             int i = num % 10;
14             
15             if (i > 0) {
16                 result = string(roman_table[level][i - 1]) + result;
17             }
18             
19             num /= 10;
20             ++level;
21         }
22         
23         return result;
24     }
25 };
原文地址:https://www.cnblogs.com/panda_lin/p/integer_to_roman.html