美文网首页
Lintcode418 Integer to Roman so

Lintcode418 Integer to Roman so

作者: 程风破浪会有时 | 来源:发表于2018-02-06 09:53 被阅读0次

    【题目描述】

    Given an integer, convert it to a roman numeral.

    The number is guaranteed to be within the range from 1 to 3999.

    给定一个整数,将其转换成罗马数字。

    返回的结果要求在1-3999的范围内。

    【题目链接】

    www.lintcode.com/en/problem/integer-to-roman/

    【题目解析】

    根据罗马数字的表示方法,我们直到罗马字符有”M”, “CM”, “D”, “CD”, “C”, “XC”, “L”, “XL”, “X”, “IX”, “V”, “IV”, “I”这几种,分别对应1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1。如果在这些数字之间的,比如3,那就重复3次写1:III。

    所以只需要按照罗马数字表示的从大到小顺序,当num比a[i]大的时候,cnt = num / a[i],就将b[i]连接在result字符串后面cnt次。每一次循环将num取余a[i],直到num<=0为止

    【参考答案】

    www.jiuzhang.com/solutions/integer-to-roman/

    相关文章

      网友评论

          本文标题:Lintcode418 Integer to Roman so

          本文链接:https://www.haomeiwen.com/subject/tjmngxtx.html