[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 ...
随机推荐
- nth-child和nth-of-type的区别
以前一般都用:nth-child,后来知道了:nth-of-type,然后就一般用nth-of-type 它们两有什么区别呢? 首先来看个现象: 假如有这样一个HTML结构 <div class ...
- Android常用组件Broadcast介绍
一.Broadcast简介 Broadcast是Android的四大组件之一.可分为: 1.普通广播 发送一个广播,所有监听该广播的广播接收者都可以监听到改广播. 2.异步广播 当处理完之后的Inte ...
- 从远程oracle上导入到本地同一张表中不存在的记录的方法
场景:在远程oracle上存在一张表A,在本地同样存在一张相同表结构的表B.由于本地表B中保存了业务系统操作产生的几条记录,同时原来导入了A中的部分记录,但是并没有保存A中全部的记录.A中有15条记录 ...
- 【ecos学习1】wmware运行redboot[方法一]--脚本实现配置
背景: 远程服务器Ubuntu生成软盘镜像,通过Mac下wmware运行. 1- 环境及版本: uname -a 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 ...
- hdu 4906 3-idiots fft
题目链接 n个火柴棍取3个, 问能组成三角形的概率是多少. kuangbin大神的博客写的很详细了..http://www.cnblogs.com/kuangbin/archive/2013/07/2 ...
- xml文件的解析
1. xml文件的解析 void CDataMgr::readStringData() { std::string xml_name = "config/string.xml"; ...
- python成长之路第三篇(2)_正则表达式
打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.什么是正则表达式,python中得正则简介 2.re模块的内容 3.小练习 一.什么是正则表达式(re) 正则表 ...
- 补丁惹的祸-ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService
未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出 问题: 重新安装了VS2012,结 ...
- Git学习03 --远程仓库
把本地库的内容推送到远程(github), 用git push命令,实际上是把当前分支master推送到远程. 由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的 ...
- 使用TypeScript开发一个在线记事本,支持离线存储
先贴上源码传送门: https://github.com/flowforever/yaryin.note 记事本网址: http://yindoc.com , 井号后面写你喜欢的文件名即可. 最近在研 ...