zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):012-Integer to Roman

    题目来源:

    https://leetcode.com/problems/integer-to-roman/


    题意分析:

          这道题是要把在区间[1-3999]的数字转化成罗马数字。


    题目思路:

          只要知道了罗马数字和阿拉伯数字是怎么转换的就不难了,要注意的是900,500,400,90,50,40,9,5,4分别应该是‘CM’,‘D’,‘CD’,‘XC’,‘L’,‘XL’,‘IX’,‘V’,‘IV’。


    代码(python):

     1 class Solution(object):
     2     def intToRoman(self, num):
     3         """
     4         :type num: int
     5         :rtype: str
     6         """
     7         a = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
     8         b = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
     9         ans = ''
    10         i = 0
    11         count = 0
    12         while num > 0:
    13             count = num/a[i]
    14             num %= a[i]
    15             while count > 0:
    16                 ans += b[i]
    17                 count -= 1
    18             i += 1
    19         return ans
    View Code

    转载请注明出处:http://www.cnblogs.com/chruny/p/4817819.html

  • 相关阅读:
    Mockito 简明教程
    说说初用 Mock 工具测试碰到的坑
    java的mock测试框架
    使用模拟对象(Mock Object)技术进行测试驱动开发
    微信小程序
    微信小程序
    uniapp
    微信小程序
    微信小程序
    微信小程序
  • 原文地址:https://www.cnblogs.com/chruny/p/4817819.html
Copyright © 2011-2022 走看看