LeetCode 12 Integer to Roman

来源:互联网 发布:曼秀雷敦润唇啫喱知乎 编辑:程序博客网 时间:2024/05/29 03:36

题意:

把数字变成罗马数字。


思路:

简单模拟。为了把代码写漂亮点还是可以用用函数什么的。


代码:

//// Created by house on 1/9/17.//class Solution {public:    string intToRoman(int num) {        stringstream ss;        int thousand = num / 1000;        while (thousand--) {            ss << "M";        }        func(ss, num / 100 % 10, "M", "D", "C");        func(ss, num / 10 % 10, "C", "L", "X");        func(ss, num % 10, "X", "V", "I");        return ss.str();    }private:    void func(stringstream &ss, int num, string ten, string five, string one) {        if (num == 4) {            ss << one << five;        } else if (num == 9) {            ss << one << ten;        } else {            if (num >= 5) {                ss << five;                num -= 5;            }            while (num--) {                ss << one;            }        }    }};


0 0
原创粉丝点击