[leetcode] 12. Integer to Roman

来源:互联网 发布:方德软件中心 编辑:程序博客网 时间:2024/06/06 00:39

Given an integer, convert it to a roman numeral.

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

解法一:

首先要明确罗马数字的基本规则:

I V XL C D M

1 5 1050 100 500 1000


class Solution {public:    string intToRoman(int num) {        vector <int> values = {1000,900,500,400,100,90, 50, 40, 10, 9, 5, 4, 1};        vector <string> str = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};        string res;        for(int i=0; i<values.size(); i++){            while(num>=values[i]){                num -= values[i];                res += str[i];            }        }        return res;    }};


0 0
原创粉丝点击