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. 关于group by

    <pre name="code" class="sql">关于group by 排序问题 10g 以前sort group by 需要排序 10g ...

  2. Linux企业级项目实践之网络爬虫(9)——通过URL抓取网页内容

    基本URL包含模式(或称协议).服务器名称(或IP地址).路径和文件名,如"协议://授权/路径?查询".完整的.带有授权部分的普通统一资源标志符语法看上去如下:协议://用户名: ...

  3. C++ int 转换成 string intToString

    string intToString(int num) { stringstream ss; ss<<num; return ss.str(); } 一个简单的小例子. #include ...

  4. 在 Ubuntu 12.04 上安装 GitLab6.0

    安装环境: 操作系统:    Ubuntu 12.4 LTS 英文 数据库:        mysql5.5.32 web服务器: nginx1.4.1 首先, 添加git和nginx的ppa,并升级 ...

  5. poj 1274 The Perfect Stall(二分图匹配)

    Description Farmer John completed his new barn just last week, complete with all the latest milking ...

  6. neural style论文解读

    相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 概述 在艺术领域,艺术家 ...

  7. eclipse 404以及tomcat failed to start错误

    eclipse中的servlet项目有时会不编译,不编译可能就会出现404错误,因为在build path的输出目录并没有class文件,然而如果在输出目录引入之前编译的class文件,就可能出现cl ...

  8. edmx文件

    MethodBase 提供有关方法的信息 在System.Reflector命名空间之下 edmx edmx:Runtime节点下包含与EF有关的定义与映射信息 edmx:ConceptualMode ...

  9. 工程与科学数值方法的Matlab实现

    %stats.m function [mean,stdev]=stats(x) n=length(x);mean=sum(x)/n;stdev=sqrt(sum((x-mean).^2/(n-1))) ...

  10. BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 算法讨论: 1.可以用最大流做,最大流等于最小割. 2.可以把这个图转化其对偶图,然 ...