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 ...
随机推荐
- NSIS操作系统环境变量
手头有个项目需要修改PATH变量 需要!include "EnvVarUpdate.nsh" 以下是NSIS脚本代码 ; Script generated by the HM NI ...
- Lambda 表达式的示例-来源(MSDN)
本文演示如何在你的程序中使用 lambda 表达式. 有关 lambda 表达式的概述,请参阅 C++ 中的 Lambda 表达式. 有关 lambda 表达式结构的详细信息,请参阅 Lambda 表 ...
- C++基础回顾2(函数, 指针和引用)
接着回顾函数.指针和应用. 函数 1.多维数组作为形参时,第一维的大小可以省略(也可以不省略),但是其他维的大小必须指定.比如二维数组形参,int array[3][]不正确,int arry[][1 ...
- amcharts报表制作
fusioncharts官网:http://www.fusioncharts.com/demos/gallery/#column-and-bar 最近使用amcharts fb4,flashBuild ...
- Xcode7中你一定要知道的炸裂调试神技(转)
1.Address Sanitizer: 妈妈再也不用担心 EXC_BAD_ACCESS? EXC_BAD_ACCESS一直是很多开发者的噩梦,因为这个错误很不直观,出现后往往要花很长时间才能定位到错 ...
- 使用面向 iOS 的本机插件扩展
本文细致探讨了 Xcode(以 iOS 设备为目标)中的 PhoneGap(也称为 Apache Cordova)应用程序本机插件.如果您刚开始接触 PhoneGap 或者需要回顾 PhoneGap ...
- Unity怎样在Editor下运行协程(coroutine)
在处理Unity5新的AssetBundle的时候,我有一个需求,须要在Editor下(比方一个menuitem的处理函数中,游戏没有执行.也没有MonoBehaviour)载入AssetBundle ...
- C-冒泡排序,选择排序,数组
——构造类型 ->数组 ->一维数组 ->相同类型的一组数据 ->类型修饰符--数组名—[数组的元素个数(必须是整型表达式或者是整型常量,不能是变 ...
- Java第四周学习日记
Day01 双列集合: 在现实生活中有些数据是以映射关系存在的,也就是成对存在的,比如夫妻等.单例集合无法表现出映射关系,所以学习双列集合. 双列集合无迭代器. 1.Map 双列集合: ——————— ...
- redis主从 哨兵
entinel是redis高可用的解决方案,sentinel系统(N个sentinel实例,N >= 1)可以监视一个或者多个redis master服务,以及这些master服务的所有从服务: ...