LeetCode-012 Integer to Roman

来源:互联网 发布:qq的smtp端口号 编辑:程序博客网 时间:2024/05/16 23:55

Description

Given an integer, convert it to a roman numeral.

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

Analyse

没啥好分析的,水题一道,阿拉伯数字转罗马数字。

Code

class Solution {public:    const int value[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};    const string strs[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};    string intToRoman(int num) {        string ans;        for (int i=0;i<13;i++) {            while (num>=value[i]) {                num-=value[i];                ans=ans+strs[i];            }        }        return ans;    }};
原创粉丝点击