【LeetCode 12】整数转罗马数字

题目链接

【题解】


(涨知识了。。原来罗马数字是这么回事。。
把{1,4,5,9,10,40,50,90,100,400,500,900,1000}这些东西的罗马数字放在一个数组里面。
每次从大到小减。
减一个加上对应的罗马数字就好。

【代码】

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

猜你喜欢

转载自www.cnblogs.com/AWCXV/p/11802433.html