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(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 hereif(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 hereint[] 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 representationdef intToRoman(self, n):# Write your code herenumbers = [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]*countreturn "".join(res)
Python Code
参考链接
转载于:https://www.cnblogs.com/theskulls/p/4957173.html
lintcode :Integer to Roman 整数转罗马数字相关推荐
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", &q ...
- lintcode 418整数转罗马数字
描述 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 说明 https://en.wikipedia.org/wiki/Roman_numerals https://zh. ...
- Integer to Roman 问题
Integer to Roman 问题 leetcode java 1.问题描述 Given an integer, convert it to a roman numeral.Input is gu ...
- Leetcode No.12 整数转罗马数字
题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X ...
- 算法:整数转罗马数字(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 ,即为两个并 ...
- leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换
12 Integer to Roman 13 Roman to Integer 有可能不注意的结果: class Solution {public:/*1.相同的数字连写,所表示的数等于这些数字相加得 ...
- LeetCode算法入门- Roman to Integer Integer to Roman -day8
LeetCode算法入门- Roman to Integer -day8 Roman to Integer: 题目描述: Roman numerals are represented by seven ...
- (补)20200105:整数转罗马数字
整数转罗马数字 题目 大致思路 代码实现 题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 ...
- Roman to Integer/Integer to Roman
1 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...
最新文章
- AsyncTask与多任务
- P2870 [USACO07DEC]Best Cow Line G
- 工作74:vue带参数跳转其他页面
- python中的__new__概念(工厂
- vsftpd的基于pam_mysql的虚拟用户机制实现数据传输
- Ubuntu系列硬盘安装
- Mysql 哈希索引
- DAY 5 综合练习 - 学籍注册小程序 棋牌游戏发牌程序
- 产品需求文档模板,不用找了(附“简”例)【转】
- 错误: 在类中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则
- 如何用运营思维,搭建会员运营体系
- 威联通NAS配置阿里云域名和SSL证书
- 持续交付和DevOps是一对好基友
- 如何下载Google Chromium源码。
- 基于 Vue 2.0 的 UI 组件库 KUI for Vue
- Windows XP 启动过程jjhou
- 纯干货,PSI 原理解析与应用
- php用do while实现斐波那契数列,php实现斐波那契数列
- 移动开发者如何获取免费流量
- Excel 实现多列文本合并/合并单元格内容 的三种方法
热门文章
- thinkphp第一节结构
- 删除Windows Service
- elupload获取文件名与路径_Uipath获取文件名,路径,扩展名等操作
- 以OpenCV为例配置VS第三方库
- vs2008下设置.h, .lib和 .dll 的路径配置全图及其意义
- juyter显示决策树图形_决策树分析细分市场
- c++可以做什么项目_上班做下班后可以做的兼职项目
- js正则 匹配 正则表达式
- GIS实战应用案例100篇(三)-基于NDVI指数的绿地信息提取
- java创建树结构_Java学习之XML-017