Integer to Roman

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 intToRoman( int num) {
         int mp[ 100];
         int val[]={ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
         string r[]={ " M ", " CM ", " D ", " CD ", " C ", " XC ", " L ", " XL ", " X ", " IX ", " V ", " IV ", " I "};
        mp[ ' I ']= 1;
        mp[ ' V ']= 5;
        mp[ ' X ']= 10;
        mp[ ' L ']= 50;
        mp[ ' C ']= 100;
        mp[ ' D ']= 500;
        mp[ ' M ']= 1000;
        
         int index= 0;
         string result= "";
         while(num> 0)
        {
             while(num<val[index])
                index++;
            result=result+r[index];
            num=num-val[index];
        }
         return result;
    }
};  

 

你可能感兴趣的:(Integer)