leetcode:Integer to Roman(整数转化为罗马数字)
Question:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
给你一个整数,把它转化为罗马数字,输入保证在1到3999之间。
关于罗马数字介绍:
1、计数方法:① 罗马数字就有下面七个基本符号:Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、L(50)、C(100)、D(500)、M(1000)。
② 相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;XX=20;CC=200;MMM=3000;
③ 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
④ 小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
⑤ 正常使用时连写的数字重复不得超过三次;
2、组数规则: ① 基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个。
② 不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目;
③ V 和X 左边的小数字只能用Ⅰ。
④ L 和C 左边的小数字只能用X。
⑤ D 和M 左边的小数字只能用C。
3、对照举例: ① 个位举例:Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ,6】Ⅶ,7】 Ⅷ,8 】Ⅸ,9 】
② 十位举例:Ⅹ,10】 Ⅺ,11 】Ⅻ,12】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】
③ 百位举例:C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
④ 千位举例:M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999
算法基本思想: 这个题仅仅要求到3999比较简单,根据罗马数字的构建数字的规则,无论个位十位百位还是千位都有相同的构造方法,但是所表示的字母是不同的,所以我想到去写一个函数,public String intToCharacter(int n,int i),n表示数位上的数字,i表示要转化的整数从右到左第几位,这个函数可以把单独的数字转化成罗马字母,在public String intToRoman(int num) 函数中,传入要转化的整数num,然后依次从右向左取出各位,依次求相应罗马数字,并进行字符串拼接,就能得到所需要的结果。
代码实现(java):
class Solution {
public String intToRoman(int num) {
String str="";
int i=1,n; //i表示从右到左整数第几位
while(num>0){
n=num%10; //表示数位上的数字
num/=10;
str=intToCharacter(n, i).concat(str); //把每一位返回的字符串和str相连
i++;
}
return str;
}
public String intToCharacter(int n,int i){//n表示特定的输入,i表示个位十位百位或者千位
String str1,str2,str3;//拿0~9之间的数举例,str1表示I,str2表示V,str3表示X
String str="";//这个函数返回特定位上的罗马数字表示形式
if(1==i){ //下面if语句是判断n是哪一位上的,依次用罗马数字表示1、5、10
str1="I"; //个位上的数
str2="V";
str3="X";
}
else if(2==i){
str1="X"; //十位上的数
str2="L";
str3="C";
}else if(3==i){
str1="C"; //百位上的数
str2="D";
str3="M";
}else{
str1="M"; //千位上的数,因为最大数到3999,所以str2和str3为空
str2="";
str3="";
}
switch(n){ //switch对各数字进行组合以个位上的数举例如下
case 0:break; //空
case 1:str+=str1;break; //I
case 2:str+=str1+str1;break; //II
case 3:str+=str1+str1+str1;;break;//III
case 4:str+=str1+str2;break; //IV
case 5:str+=str2;break; //V
case 6:str+=str2+str1;break; //VI
case 7:str+=str2+str1+str1;break; //VII
case 8:str+=str2+str1+str1+str1;break; //VIII
case 9:str+=str1+str3;break; //IX
}
return str;
}
}
leetcode:Integer to Roman(整数转化为罗马数字)的更多相关文章
- [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 ...
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- [Leetcode] Interger to roman 整数转成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 012 Integer to Roman 整数转换成罗马数字
给定一个整数,将其转为罗马数字.输入保证在 1 到 3999 之间. 详见:https://leetcode.com/problems/integer-to-roman/description/ cl ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- LeetCode: Integer to Roman 解题报告
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within 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 ...
随机推荐
- android sqlite 怎么写入存储时间
字符串类型2013-12-10 12:12:12 SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd"); ...
- Python命令行解析库argparse
2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse模块对命令行进行解析. 1.example 有一道面试题:编写一个脚本main.py,使用方式如下: ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...
- MS UI Automation Introduction
MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自 ...
- 导出到Excel并且取消默认的科学计算法
导出Excel的代码很多,其中这种最简单: protected void btnDCAll_Click(object sender, EventArgs e) { ...
- plsql programming 19 触发器
挂起语句, 是指数据库 Hang 到那不能动了, 触发的. 1. DML 触发器 这种类型的触发器对于开发人员都很常见, 其他类型的触发器主要是给DBA使用的. 配置触发器,我们需要回答以下问题: 触 ...
- tune 06 Database Configuration and I/O Issues
1. 尽量读内存中的数据 2. 尽量减少IO矛盾, 即多个任务同时读写一块磁盘 表和索引要尽量分开在不同磁盘, 因为表和它的索引是同时读取的, 所以分磁盘后, 对性能会提高. 物理磁盘的调优相关 re ...
- How to use 'crontab' command on bitnami
You can edit the cron file using the following command: $ sudo crontab -e You can add a new line lik ...
- NPOI导出Excel表功能实现(多个工作簿)(备用)
Excel生成操作类: 代码 using System; using System.Collections.Generic; using System.Text; using System.IO; u ...
- CSS 中浮动的使用
float none 正常显示 left 左浮动 right 右浮动 clear none 允许两边浮动 left 不允许左边浮动 right 不允许右边浮动 both 不允许两边浮动 <!DO ...