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 ...
随机推荐
- 【转】Math.Atan2 方法
原文网址:https://msdn.microsoft.com/zh-cn/library/system.math.atan2.aspx 返回正切值为两个指定数字的商的角度. 命名空间: Syste ...
- ZOJ2317-Nice Patterns Strike Back:矩阵快速幂,高精度
Nice Patterns Strike Back Time Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...
- Visual Studio 2012 编译C++显示cl命令
为了用newlisp来实现VC编译,以便用我的Emacs开发VC程序,而不需要再打开VS 2012, 需要自己实现命令行的编译.我不需要nmake,因为我想直接了解VC编译器,以便今后更好的驾驭它. ...
- MVC MVC 路由详解
在项目中我们引用了System.Web.Routing; Routing的作用: 确定Controller 确定Action 确定其他参数 根据识别出来的数据, 将请求传递给Controller和 ...
- 使用python监听、模拟鼠标键盘事件
最近守望职业选手疑似开挂事件挺热闹的,在下小菜一枚,并不能从视频中看出端倪.看了一些关于外挂的讨论,自动点射和压枪只需在鼠标驱动上做些改动即可,自瞄或其他高级功能则需要读内存或修改游戏文件,检测也更容 ...
- Laravel-表单篇-零散信息
1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...
- laravel-模板引擎Blade
(慕课网_轻松学会Laravel-基础篇_天秤vs永恒老师) 一.概述 Blade是Laravel提供的一个既简单又强大的模板引擎 和其他流行的PHP模板引擎不一样,Blade并不限制你在视图view ...
- cobol语言基础培训教程
COBOL 是Common Business Oriented Language 的缩写.它不仅是商业数据处理的理想语言,而且广泛用于数据管理领域,因此COBOL 语言也被称为”用于管理的语言”. 一 ...
- The secret of ROWID
表里每个数据行都有一个行头部,在这里存放了该行数据所包含的列的数量,以及锁定标记等.当某个事务更新某条记录时,会在该数据行的头部记录所用到的ITL槽号以及锁定标记.接下来则是列长度以及列的值.Orac ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...