Integer to Roman [LEETCODE]

Given an integer, convert it to a roman numeral.

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

======================================

 1 class Solution {
 2 public:
 3     string intToRoman(int num) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.    
 5         string roman_char[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","III","II","I"};
 6         int roman_value[]   ={1000,900, 500,400, 100, 90,  50, 40,  10,  9,   5,  4,    3,   2,   1};
 7         string result = "";
 8         for(int i = 0; i < 15; i++){
 9             if(num >= roman_value[i]){
10                 num -= roman_value[i];
11                 result += roman_char[i];
12                 i = -1;
13             }
14         }
15         return result;
16     }
17 };

posted @ 2013-10-21 16:17  昱铭  阅读(135)  评论(0)    收藏  举报