题解 | #牛牛和罗马数字智力游戏#

牛牛和罗马数字智力游戏

https://www.nowcoder.com/practice/7f4bd3b2d7d34f5c87d84120d9782c1d

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param num int整型 
     * @param limit int整型 
     * @return string字符串
     */
    vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    vector<int> divisor{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
    string integerToRomanWithReverse(int num, int limit) {
        // write code here
        string ans = "";
        int index = 0;
        while (num) {
            int quotient = num / divisor[index];
            while (quotient--) {
                ans += str[index];
            }
            num %= divisor[index];
            index++;
        }
        if (ans.size() >= limit) {
            reverse(ans.begin(), ans.end());
        }
        return ans;
    }
};

全部评论

相关推荐

爱写代码的菜code...:哎,自己当时拿到字节offer的时候也在感叹终于拿到了,自己当时最想去的企业就是字节,结果还是阴差阳错去了鹅厂。祝uu一切顺利!!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务