题目

给定一个整数,将其转换成罗马数字。

返回的结果要求在1-3999的范围内。

样例

4 -> IV

12 -> XII

21 -> XXI

99 -> XCIX

更多案例,请戳 http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm

说明

什么是 罗马数字?

解题

拼写规则

罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的规则可以表示任意正整数。需要注意的是罗马数字中没有“0”,与进位制无关。一般认为罗马数字只用来记数,而不作演算。
重复数次:一个罗马数字重复几次,就表示这个数的几倍。
右加左减:
在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
但是,左减时不可跨越一个位数。比如,99不可以用IC(100-1)表示,而是用XCIX(100-10  10 -1)表示。(等同于阿拉伯数字每位数字分别表示。)
左减数字必须为一位,比如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 整数转罗马数字的更多相关文章

  1. [LintCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...

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

  3. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  4. 【LeetCode】Integer to Roman(整数转罗马数字)

    这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...

  5. [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 ...

  6. 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 ,即为两个并 ...

  7. Leetcode12--->Integer to Roman(整数转换为罗马数字)

    题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 ...

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

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

  9. [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 ...

随机推荐

  1. LitJSON使用

    地址:http://lbv.github.io/litjson/docs/quickstart.html LitJSON Quickstart Guide Introduction Quick Sta ...

  2. Linux下vsftpd搭建过程(防火墙版)

    1.确认主机IP [root@www data]# ifconfig  eth0      Link encap:Ethernet  HWaddr 00:0C:29:22:05:B8         ...

  3. PHP 图片文件上传代码

    通过 PHP,可以把文件上传到服务器.里面加入一些图片的判断,如果不加判断文件的类型就可以上传任意格式的文件. 为了网站的安全,肯定不让上传php文件,如果有人进入你的后台,上传了一个php文件,你的 ...

  4. centos系统下安装使用composer教程

    Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.Composer 不是一个包管理器.是的,它涉及 "packages" ...

  5. linux发展前景如何?

    2014-01-09 18:54Linux将不会取代Windows成为主流的桌面操作环境, 但它很有可能在信息接入设备中独霸天下. 为什么Linux无法取代Windows呢?最主要的原因是大多数最终用 ...

  6. MySQL数据库主从复制

    一.MySQ主从复制(主库写入数据,从库读取数据) MySql官方下载地址:http://dev.mysql.com/downloads/mysql/ MySql常用命令: 设置密码 UPDATE U ...

  7. C# 链接Sql和Access数据库语句

    1.sql数据库: 1.1.链接数据语句:server=localhost;database=Data; uid=sa;pwd=123; 或 Data Source=localhost;DataBas ...

  8. MySQL参数调优最佳实践

    前言很多时候,RDS用户经常会问如何调优RDS MySQL的参数,为了回答这个问题,写一篇blog来进行解释: 哪一些参数不能修改,那一些参数可以修改:这些提供修改的参数是不是已经是最佳设置,如何才能 ...

  9. iOS开发应用学习笔记

    一.iOS应用设计 1. 参考资料: 解读iPhone平台的一些优秀设计思路 iPhone App的特点及基本设计方法 Mobile UI design and Developer 2. 用户对iPh ...

  10. 野指针及c++指针使用注意点

    避免野指针的产生 “野指针”的成因主要有: 1)指针变量没有被初始化.任何指针变量刚被创建时不会自动成为NULL指针,它的缺省值是随机的,它会乱指一气.所以,指针变量在创建的同时应当被初始化,要么将指 ...