码迷,mamicode.com
首页 > 其他好文 > 详细

Integer to Roman

时间:2015-05-11 21:55:30      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   

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) {
    	map<int,string> mp = {{1000,"M"},{900,"CM"},{500,"D"},
    			{400,"CD"},{100,"C"},{90,"XC"},{50,"L"},{40,"XL"},{10,"X"},
				{9,"IX"},{5,"V"},{4,"IV"},{1,"I"}};
    	string res;
    	for(auto iter=mp.rbegin();iter!=mp.rend();++iter){
    		while(num-iter->first>=0){
    			res += iter->second;
    			num -= iter->first;
    		}
    	}
    	return res;
    }
};


Integer to Roman

标签:c++   leetcode   

原文地址:http://blog.csdn.net/guorudi/article/details/45648807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!