日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

整数转罗马数字 python

發布時間:2025/4/5 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 整数转罗马数字 python 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文鏈接

一個容易理解的方法
class Solution(object):def intToRoman(self, num):""":type num: int:rtype: str"""tmp = []roman = ''if not num:raise ValueErrorwhile num >= 1000:num -= 1000tmp.append('M')while num >= 900:num -= 900tmp.append('CM')while num >= 500:num -= 500tmp.append('D')while num >= 400:num -= 400tmp.append('CD')while num >= 100:num -= 100tmp.append('C')while num >= 90:num -= 90tmp.append('XC')while num >= 50:num -= 50tmp.append('L')while num >= 40:num -= 40tmp.append('XL')while num >= 10:num -= 10tmp.append('X')while num >= 9:num -= 9tmp.append('IX')while num >= 5:num -= 5tmp.append('V')while num >= 4:num -= 4tmp.append('IV')while num >= 1:num -= 1tmp.append('I')return roman.join(tmp) class Solution(object):def intToRoman(self, num):""":type num: int:rtype: str"""tmp = []hashmap = {'I': 1, 'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900, 'V': 5,'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}sort_hashmap = sorted(hashmap.items(), key=lambda _: _[1], reverse=True)roman = ''for i in range(len(sort_hashmap)):while num>=sort_hashmap[i][1]:num -= sort_hashmap[i][1]tmp.append(sort_hashmap[i][0])return roman.join(tmp)

總結

以上是生活随笔為你收集整理的整数转罗马数字 python的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。