热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

12.IntegertoRoman整数转罗马数字

Givenaninteger,convertittoaromannumeral.Inputisguaranteedtobewithintherangefrom

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
     def intToRoman(self, num):
         "" "
         :type num: int
         :rtype: str
         "" "
         I = [ '' , 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX' ]
         X = [ '' , 'X' , 'XX' , 'XXX' , 'XL' , 'L' , 'LX' , 'LXX' , 'LXXX' , 'XC' ]
         C = [ '' , 'C' , 'CC' , 'CCC' , 'CD' , 'D' , 'DC' , 'DCC' , 'DCCC' , 'CM' ]
         M = [ '' , 'M' , 'MM' , 'MMM' ]
         thousand = num // 1000
         houndred = (num % 1000 ) // 100
         ten = (num % 100 ) // 10
         digit = num % 10
         return M[thousand] + C[houndred] + X[ten] + I[digit]
  
  
s = Solution()
# num = 1
# num = 19
# num = 99
# num = 100
num = 409
#num = 1999
res = s.intToRoman(num)
print(res)




来自为知笔记(Wiz)



推荐阅读
author-avatar
mobiledu2502890777
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有