[LeetCode]Integer to Roman

Given an integer, convert it to a roman numeral.

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

思考:罗马数字有I,V,X,L,C,D,M。其中4,9左减,其他右加。

class Solution {
public:
    string intToRoman(int num) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
		if(num<1||num>3999) return NULL;
        const int integer[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        const string roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        string ret="";
		int i;
		for(i=0;i<13;i++)
		{
			while(num>=integer[i])
			{
				num-=integer[i];
				ret+=roman[i];			
			}
		}
		return ret;
    }
};

  

原文地址:https://www.cnblogs.com/Rosanna/p/3413985.html