Roman numerals
Roman numerals
罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, 90)->('X', 'XL', 'L', 'XC')
1 def checkio(data):
2 rel = ''
3
4 thonsand = data / 1000
5 rel += thonsand * 'M'
6
7 data %= 1000
8
9 table = [['C', 'CD', 'D', 'CM'], ['X', 'XL', 'L', 'XC']]
10
11 pos = 100
12
13 for i in range(0, 2):
14 bit = data / pos
15 if bit > 0:
16 if bit < 4:
17 rel += bit * table[i][0]
18 elif bit == 4:
19 rel += table[i][1]
20 elif bit == 5:
21 rel += table[i][2]
22 elif bit == 9:
23 rel += table[i][3]
24 else:
25 rel += (table[i][2] + table[i][0] * (bit - 5))
26
27 data %= pos
28 pos /= 10
29
30 if data > 0:
31 unit = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
32 rel += unit[data - 1]
33
34 #replace this for solution
35 return rel
另外还需注意没有个位数的情况, 即第30行所示
观摩JulianNicholls的代码
1 elements = { 1000 : 'M', 900 : 'CM', 500 : 'D', 400 : 'CD',
2 100 : 'C', 90 : 'XC', 50 : 'L', 40: 'XL',
3 10 : 'X', 9 : 'IX', 5 : 'V', 4: 'IV', 1 : 'I' }
4
5 def checkio(data):
6 roman = ''
7
8 for n in sorted(elements.keys(), reverse=True):
9 while data >= n:
10 roman += elements[n]
11 data -= n
12
13 return roman
sorted(elements.keys(), reverse=True), 按key从大到小排列; 思路不错, 比数位对应的值大, 即加上该数位对应的字母, 并减去对应的数值, 直到data为0
Roman numerals的更多相关文章
- Project Euler 89:Roman numerals 罗马数字
Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...
- Project Euler:Problem 89 Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...
- CodeForcesGym 100641D Generalized Roman Numerals
Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on ...
- Roman Numerals All In One
Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...
- UVA - 185 Roman Numerals
题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Roman to Integer -- LeetCode 13
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
随机推荐
- 转:linux执行shell脚本的方式及一些区别
假设shell脚本文件为hello.sh放在/root目录下.下面介绍几种在终端执行shell脚本的方法: [root@localhost home]# cd /root/ [root@localho ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- 【转】Android通过JNI调用驱动程序(完全解析实例)
原文网址:http://blog.csdn.net/ok138ok/article/details/6560875 要达到的目的:android系统中,用JAVA写界面程序,调用jni中间库提供的接口 ...
- 代码高亮插件Codemirror使用方法及下载
代码高亮插件Codemirror使用方法及下载 - 老男孩的日志 - 网易博客 代码高亮插件Codemirror使用方法及下载 2013-10-31 16:51:29| 分类: 默认分类 | ...
- 如何备份及恢复Linux文件权限
你可能听说或碰到过这样的事情:一个系统管理员菜鸟不小心输入"chmod -R 777 /"从而导致了巨大的悲剧,使得整个系统遭到了严重的破坏.在日常管理中,我们有许多工具可以用来备 ...
- 第33讲 UI组件_进度条ProcessBar和消息队列处理器handler
第33讲UI组件_进度条ProcessBar和消息队列处理器handler 1. 进度条ProcessBar 一个可视化的进度指示器,代表正在执行的耗时任务.可以为用户展示一个进度条,表示正在执行的任 ...
- Android开发有用的站点
在github上面找到一个个人认为比較好的站点,好在能够方便下载开发工具.我的AndroidStudio就是在上面下载的.安装了一直在使用.该 网址主要收集整理Android开发所需的Android ...
- oracle数据库事务相关【weber出品必属精品】
事务的概念:事务:一个事务由一组构成一个逻辑操作的DML语句组成 事务有开始有结束,事务以DML语句开始,以Conmmit和Rollback结束.以下情况会使得事务结束: 1. 执行COMMIT 或者 ...
- iOS_SN_沙盒文件操作及位置
转载:http://blog.csdn.net/hello_hwc/article/details/44916909 沙盒的结构如下所示 一 访问Bundle 注意Bundle只读,不能写入 创建一个 ...
- [总结] Stack: Java V.S. C++
小结一下Stack 的主要API操作. 在c++ 和 java 中,stack 的操作几乎相同,只有查询栈顶元素一项操作的名称不同 (top() v.s. peek()) . 此外,在构造函数中,Ja ...