leetcode - Integer to Roman

来源:互联网 发布:linux如何查看ftp密码 编辑:程序博客网 时间:2024/05/10 05:06

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


0 0