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的更多相关文章

  1. Project Euler 89:Roman numerals 罗马数字

    Roman numerals For a number written in Roman numerals to be considered valid there are basic rules w ...

  2. 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 ...

  3. CodeForcesGym 100641D Generalized Roman Numerals

    Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on  ...

  4. Roman Numerals All In One

    Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...

  5. UVA - 185 Roman Numerals

    题目链接: https://vjudge.net/problem/UVA-185 思路: 剪枝.回溯 注意回溯的时候,是从当前点的下一个开始,而不是从已经遍历的个数点开始!!不然回溯有问题! 思路参考 ...

  6. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  7. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  8. 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 ...

  9. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

随机推荐

  1. V$LATCH_PARENT和V$LATCH_CHILDREN

    V$LATCH_PARENT contains statistics about parent latches. The columns of V$LATCH_PARENT are identical ...

  2. Flash AIR 保存与读取

    package { import flash.display.Sprite; import flash.net.URLLoader; import flash.display.Loader; impo ...

  3. 怎样用jQuery自带方法/函数来获取outerHTML属性

    原文地址:http://jingyan.baidu.com/article/7f41ececf93b48593d095c25.html 包括我自己在内(其实我也就这两天才知道这样可以快速获取的),很多 ...

  4. C#的隐式和显示类型转换

    原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/7494577 关于隐式转换和显示转换,每种语言都有的,C#中当然也不例外. ...

  5. C#中单问号,双问号的用法(转)

    原文:http://hi.baidu.com/guodong828/blog/item/c78fc23f847314cb7d1e7193.html 单问号---用于给变量设初值的时候,给变量(int类 ...

  6. 前端开发利器—FIDDLER 转

    http://www.cnblogs.com/yuzhongwusan/archive/2012/07/20/2601306.html 前端开发利器—FIDDLER 1.Fiddler相对其他调试工具 ...

  7. ListView的Item被点击和其中的Button被点击同时生效

    Android开发中在ListView中经常有Button或ImageButton等需要被点击的控件,如果不加一些特殊的限制,有可能 ListView的Item的点击事件或Button的点击事件,其中 ...

  8. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

  9. node-sqlite3-API-归纳总结

    SQLITE3-API-LIST:API1. new sqlite3.Database(filename,[mode],[callback]) 返回数据库对象并且自动打开和连接数据库 它没有独立打开数 ...

  10. Action Result

    操作返回的内容成为操作结果 大多数情况下返回ViewResult,基类ActionResult 6钟标准类型: ViewResult:视图结果,包含HTML标记等元素 EmptyResult:空结果 ...