[string]Roman to Integer,Integer to Roman
一.Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
- 相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
- 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
- 小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
V 和 X 左边的小数字只能用Ⅰ。
L 和 C 左边的小数字只能用X。
D 和 M 左 边的小数字只能用C。
- 正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
- 在一个数的上面画一条横线,表示这个数扩大1000倍。
class Solution {
public:
int romanToInt(string s) {
// Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)
int values[] ={
,,,,,,,,,,,,,,,,,,,,,,,,,
};
int size = s.size();
int res = ;
for(int i=;i<size;i++){
if(i+<size){
if(values[s[i]-'A'] >= values[s[i+]-'A'])
res += values[s[i]-'A'];
else
res -= values[s[i]-'A'];
}else{
res += values[s[i]-'A'];
}
}
return res;
}
};
二.Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
string digitToRoman(int digit,int base,string kvs[][])
{
string res;
switch(digit){
case : res = kvs[base][digit];break;
case : res += kvs[base][];res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
case : res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
case : res += kvs[base][];res += kvs[base][];break;
case : res = kvs[base][];break;
case : res = kvs[base][];break;
case : res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
case : res += kvs[base][];res += kvs[base][];break;
case : res += kvs[base][];break;
default: res="";
}
return res;
}
string intToRoman(int num) {
string kvs[][];
kvs[][] = "I"; kvs[][]="IV"; kvs[][]="V"; kvs[][]="IX";
kvs[][] = "X";kvs[][] = "XL"; kvs[][]="L"; kvs[][]="XC";
kvs[][] = "C"; kvs[][] = "CD"; kvs[][]="D"; kvs[][]="CM";
kvs[][] = "M";
int d = num%;num/=;//个
int c = num%;num/=;//十
int b = num%;num/=;//百
int a = num%;num/=;//千
string res =digitToRoman(a,,kvs)+digitToRoman(b,,kvs)+digitToRoman(c,,kvs)+digitToRoman(d,,kvs);
return res;
}
};
[string]Roman to Integer,Integer to Roman的更多相关文章
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- Roman to Integer && Integer to Roman 解答
Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...
- Roman to Integer & Integer to Roman
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- Java基础之引用(String,char[],Integer)总结
1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...
- java中的BigDecimal和String的相互转换,int和String的类型转换,Integer类和String相互转换
一: /*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ 注:BigDecimal在数据库中存的是number类型. import java.math.B ...
- 批量删除以及将String数组转换成Integer数组的奇淫技巧
首先在pom.xml文件添加依赖: <!-- bean工具 --> <dependency> <groupId>commons-beanutils</grou ...
- Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题
1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...
- String,int,Integer之间的转换
public class Test{ public static void main(String[] args) { //int转换成Integer Integer in = new Integer ...
随机推荐
- Eclipse运行Tomcat7源码
1. 各环境版本: jdk1.6.0_45 (亲测jdk1.7.0_07会有问题,不要用1.7版本的) apache-ant-1.9.4 apache-tomcat-7.0.61-src 2. 安装a ...
- python-整理--使用IDE
如何使用python的IDE 安装好python3.4之后,默认有一个叫IDLE,就是目录lib/idlelib之下,是一个简单实用的工具. 在VS2013上安装一个插件就可以使用VS当IDE了.插件 ...
- Android反编译APK
http://blog.csdn.net/vipzjyno1/article/details/21039349/ 关于被加壳工具添加我的apk如何脱壳 http://www.blogfshare.co ...
- Digital Roots(hdoj1013)
Problem Description The digital root of a positive integer is found by summing the digits of the int ...
- 翻译题(map使用)
What Are You Talking About 点我 Problem Description Ignatius is so lucky that he met a Martian yesterd ...
- RESTful API实现
RESTful API实现 ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输 ...
- SQL Server 对表的 12 种一般性操作
01. 创建 create table Strings(ID int); go 02. 为表添加列 alter table Strings add String nvarchar(32); ...
- MYSQL <=>运算符
<=> 与 =
- jquery easyui datagrid 获取Checked选择行(勾选行)数据
原文:jquery easyui datagrid 获取Checked选择行(勾选行)数据 getSelected:取得第一个选中行数据,如果没有选中行,则返回 null,否则返回记录. getSel ...
- php5.5以上的版本 开启curl
对于php5.5以上的版本开启方法,需要libeay32.dll.ssleay32.dll.libssh2.dll三个文件拷备到C:\Windows目录下,php.ini中 扩展开启,重启apache ...