12、Integer to Roman

来源:互联网 发布:淘宝怎么增加流量 编辑:程序博客网 时间:2024/04/28 19:10

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) {        int numtt[4] = {0};        int index = 0;        char numeral[4][2] = {'I', 'V', 'X', 'L', 'C','D', 'M', 'M'};        while (num)        {            numtt[index++] = num % 10;            num /= 10;        }        //此题主要是弄清楚罗马数字怎么表示数字        string str;        while (index--)        {            if (numtt[index] < 4)            {                while (numtt[index]--)                    str.push_back(numeral[index][0]);            }            else if (numtt[index] == 4)            {                str.push_back(numeral[index][0]);                str.push_back(numeral[index][1]);            }            else if (numtt[index] < 9)            {                str.push_back(numeral[index][1]);                while (numtt[index] > 5)                {                    str.push_back(numeral[index][0]);                    --numtt[index];                }            }            else            {                str.push_back(numeral[index][0]);                str.push_back(numeral[index+1][0]);            }        }        return str;    }};


0 0