首页 > 其他 > 详细

【LeetCode 12】整数转罗马数字

时间:2019-11-05 23:44:01      阅读:92      评论:0      收藏:0      [点我收藏+]

题目链接

【题解】


(涨知识了。。原来罗马数字是这么回事。。
把{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;
    }
};

【LeetCode 12】整数转罗马数字

原文:https://www.cnblogs.com/AWCXV/p/11802433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!