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

时间: 2024-10-08 11:13:44

Roman numerals的相关文章

Project Euler:Problem 89 Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number

CodeForcesGym 100641D Generalized Roman Numerals

Generalized Roman Numerals Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Original ID: 100641D64-bit integer IO format: %I64d      Java class name: (Any) The ancient Romans developed a terrible numbering syste

Checkio: Roman numerals

题目: Roman numerals come from the ancient Roman numbering system. They are based on specific letters of the alphabet which are combined to signify the sum (or, in some cases, the difference) of their values. The first ten Roman numerals are: I, II, II

uva :185 - Roman Numerals(dfs)

题目:uva :185 - Roman Numerals 题目大意:给出一个字符串的等式,问这个字符串是否是罗马等式吗?有符合的阿拉伯等式吗?前者是就输出correct or incorrect ,后者就得分情况: ambiguous 能组成阿拉伯等式的字母组合大于等于2, valid 能组成阿拉伯等式的字母组合只有1种 impossible 没有符合阿拉伯等式的字母组合. 解题思路: 1.能不能组成罗马等式的规则:每个当前的字母(i)的左边的字母((i-1)所对应的值如果比i所对应的值小的话,

CSU1622: Generalized Roman Numerals(区间DP)

Description Input Output Sample Input IVX XIXIX 0 Sample Output Case 1: 4 6 Case 2: 8 10 28 30 32 HINT Source 题意:给出一个罗马数字,要你输出这罗马数字所有可能组成的数 罗马数字组成规则: 1.左边的字母大于等于右边的字母,两者相加 2.左边的字母小于右边的字母,后者减去前者 3.一个罗马数字串,可以随意组合 思路:这题要用区间dp处理,dp[i][j],代表位置i之后的j个数所能组成的

Roman Numeral Converter

将给定的数字转换成罗马数字. 所有返回的 罗马数字 都应该是大写形式. 这是一些对你有帮助的资源: Roman Numerals Array.splice() Array.indexOf() Array.join() 这个我还没做出来..

LeetCode第[13]题(Java):Roman to Integer

题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is

Facebook interview problem:13. Roman to Integer

description: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as,

13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which i