lintcode :Integer to Roman 整数转罗马数字
题目
给定一个整数,将其转换成罗马数字。
返回的结果要求在1-3999的范围内。
4
-> IV
12
-> XII
21
-> XXI
99
-> XCIX
更多案例,请戳 http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
什么是 罗马数字?
- https://en.wikipedia.org/wiki/Roman_numerals
- https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97
- http://baike.baidu.com/view/42061.htm
解题
拼写规则
重复数次:一个罗马数字重复几次,就表示这个数的几倍。
右加左减:
在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
但是,左减时不可跨越一个位数。比如,99不可以用IC(


左减数字必须为一位,比如8写成VIII,而非IIX。
右加数字不可连续超过三位,比如14写成XIV,而非XIIII。
加线乘千:
在罗马数字的上方加上一条横线或者加上下标的Ⅿ,表示将这个数乘以1000,即是原数的1000倍。
同理,如果上方有两条横线,即是原数的1000000 倍。
数码限制:
同一数码最多只能连续出现三次,如40不可表示为XXXX,而要表示为XL。
例外:由于IV是古罗马神话主神朱庇特(即IVPITER,古罗马字母里没有J和U)的首字,因此有时用IIII代替IV。
I | IV | IX | X | XL | L | XC | C | CD | D | CM | M |
1 | 4 | 5 | 9 | 10 | 40 | 50 | 90 | 100 | 500 | 900 | 1000 |
这样对任一个罗马数字可以 由上面的12个字符进行加法操作完成,即:大数在左,小数在右。《具体为什么,我不是很理解,类似于人民币只有:100,50,20,10,5,2,1,0.5,0.2,0.1可以组成任意金额》
下面程序虽然很差,但是很好理解
public class Solution {
/**
* @param n The integer
* @return Roman representation
*/
public String intToRoman(int n) {
// Write your code here
if(n<1 || n>3999)
return "ERROR";
String s="";
while(n>=1000){
s += "M";
n -= 1000;
}
while(n >= 900){
s += "CM";
n -= 900;
}
while( n>= 500){
s += "D";
n -= 500;
}
while( n>= 400){
s += "CD";
n -= 400;
}
while( n >= 100){
s += "C";
n -= 100;
}
while( n>= 90){
s += "XC";
n -= 90;
}
while( n>= 50){
s += "L";
n -= 50;
}
while( n >= 40){
s += "XL";
n -= 40;
}
while( n>= 10){
s += "X";
n -= 10;
}
while( n>= 9){
s +="IX";
n -= 9;
}
while( n>=5 ){
s += "V";
n -= 5;
}
while( n >= 4 ){
s +="IV";
n -= 4;
}
while(n >= 1 ){
s +="I";
n -= 1;
}
return s;
}
}
Java Code
然后可以改写成下面的形式
public class Solution {
/**
* @param n The integer
* @return Roman representation
*/
public String intToRoman(int n) {
// Write your code here
int[] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; String[] letters = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
String res ="" ;
for(int i = 0;i<13;i++){
if(n >= numbers[i]){
int count = n/numbers[i];
n = n%numbers[i];
for(int j=0;j<count ;j++){
res= res + letters[i];
}
}
}
return res;
}
}
Java Code
Python程序也是很简单
class Solution:
# @param {int} n The integer
# @return {string} Roman representation
def intToRoman(self, n):
# Write your code here
numbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
letters = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
res =[]
for i in range(13):
if n>= numbers[i]:
count = n/numbers[i]
n %= numbers[i]
res += letters[i]*count
return "".join(res)
Python Code
lintcode :Integer to Roman 整数转罗马数字的更多相关文章
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 【LeetCode】Integer to Roman(整数转罗马数字)
这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Leetcode12.Integer to Roman整数转罗马数字
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- Leetcode12--->Integer to Roman(整数转换为罗马数字)
题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- Linux C 程序 信号及信号的处理(19)
信号及信号的处理 1.Linux信号的介绍 信号是一种软件中断.Linux系统中根据POSIX标准扩展的信号机制. 1.信号来源 1.硬件方式 1.当用户按下某个键, ...
- js 倒计时 倒计时60秒
<input type="button" id="btn" value="免费获取验证码" onclick="settime ...
- php学习日志(4)-The mbstring extension is missing. Please check your PHP configuration错误及解决方法
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring extension is missing. Please che ...
- php读取图片内容并输出到浏览器的实现代码
如果php以图片,zip,exe等文件输出到浏览器,而前面还输出了其他字符,就会有乱码. 代码很简单,网上都能找到,但在我机子上就是显示不出来,显示出的一直是这个php文件路径,费了点时间才搞定,原来 ...
- Docker 启动失败Starting docker (via systemctl): Job for docker.service failed
Starting docker (via systemctl): Job for docker.service failed. See 'systemctl status docker.servic ...
- SQL Server 2012 BI 学习 第一天
了解数据源,数据源视图,多维数据集,维度 数据源:一个数据库或者其它数据链接,SSAS不支持使用模拟功能来处理 OLAP 对象.模拟信息选择“使用服务帐户” 数据源视图:DSV是元数据的单个统一视图, ...
- python 安装 setuptools Compression requires the (missing) zlib module 的解决方案
背景: 虚拟机centos下安装python辅助工具 setuptools报错,错误信息大概如下: Traceback (most recent call last): File "setu ...
- Win10环境下的Scrapy结合Tor进行匿名爬取
本文内容来源:http://blog.privatenode.in/torifying-scrapy-project-on-ubuntu/ 在使用Scrapy的时候,一旦进行高频率的爬取就容易被封IP ...
- Django之Model(一)--基础篇
0.数据库配置 django默认支持sqlite,mysql, oracle,postgresql数据库.Django连接数据库默认编码使用UTF8,使用中文不需要特别设置. sqlite djang ...
- boost-内存管理
Boost智能指针——scoped_ptr boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放. boost::sco ...