码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】12. Integer to Roman

时间:2016-12-22 06:49:00      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:罗马数字   put   数字转换   code   题意   ret   etc   return   integer   

Given an integer, convert it to a roman numeral.

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

题意:把数字转换为罗马数字

感觉用c的话太麻烦了,所以用Python写了

 1 class Solution(object):
 2     def intToRoman(self, num):
 3         """
 4         :type num: int
 5         :rtype: str
 6         """
 7         flag=[[‘‘,I,II,III,IV,V,VI,VII,VIII,IX],
 8             [‘‘,X,XX,XXX,XL,L,LX,LXX,LXXX,XC],
 9             [‘‘,C,CC,CCC,CD,D,DC,DCC,DCCC,CM],
10             [‘‘,M,MM,MMM]]
11         
12         i = 0
13         s = ‘‘
14         
15         while num>0:
16             t = num%10
17             s = flag[i][t]+s
18             i+=1
19             num=num//10
20             
21         return s

 

【LeetCode】12. Integer to Roman

标签:罗马数字   put   数字转换   code   题意   ret   etc   return   integer   

原文地址:http://www.cnblogs.com/fcyworld/p/6209590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!