一.Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

罗马数字是阿拉伯数字传入之前使用的一种数码。罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法:
  1. 相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
  2. 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
  3. 小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;

    V 和 X 左边的小数字只能用Ⅰ。

    L 和 C 左边的小数字只能用X。

    D 和 M 左 边的小数字只能用C。

  4. 正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
  5. 在一个数的上面画一条横线,表示这个数扩大1000倍。
  1. class Solution {
  2. public:
  3. int romanToInt(string s) {
  4. // Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)
  5. int values[] ={
  6. ,,,,,,,,,,,,,,,,,,,,,,,,,
  7. };
  8. int size = s.size();
  9. int res = ;
  10. for(int i=;i<size;i++){
  11. if(i+<size){
  12. if(values[s[i]-'A'] >= values[s[i+]-'A'])
  13. res += values[s[i]-'A'];
  14. else
  15. res -= values[s[i]-'A'];
  16. }else{
  17. res += values[s[i]-'A'];
  18. }
  19. }
  20. return res;
  21. }
  22. };

二.Integer to Roman

Total Accepted: 49661 Total Submissions: 138165 Difficulty: Medium

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

  1. class Solution {
  2. public:
  3. string digitToRoman(int digit,int base,string kvs[][])
  4. {
  5. string res;
  6. switch(digit){
  7. case : res = kvs[base][digit];break;
  8. case : res += kvs[base][];res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
  9. case : res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
  10. case : res += kvs[base][];res += kvs[base][];break;
  11. case : res = kvs[base][];break;
  12. case : res = kvs[base][];break;
  13. case : res += kvs[base][];res += kvs[base][];res += kvs[base][];break;
  14. case : res += kvs[base][];res += kvs[base][];break;
  15. case : res += kvs[base][];break;
  16. default: res="";
  17. }
  18. return res;
  19. }
  20. string intToRoman(int num) {
  21. string kvs[][];
  22. kvs[][] = "I"; kvs[][]="IV"; kvs[][]="V"; kvs[][]="IX";
  23. kvs[][] = "X";kvs[][] = "XL"; kvs[][]="L"; kvs[][]="XC";
  24. kvs[][] = "C"; kvs[][] = "CD"; kvs[][]="D"; kvs[][]="CM";
  25. kvs[][] = "M";
  26. int d = num%;num/=;//个
  27. int c = num%;num/=;//十
  28. int b = num%;num/=;//百
  29. int a = num%;num/=;//千
  30. string res =digitToRoman(a,,kvs)+digitToRoman(b,,kvs)+digitToRoman(c,,kvs)+digitToRoman(d,,kvs);
  31. return res;
  32. }
  33. };

[string]Roman to Integer,Integer to Roman的更多相关文章

  1. 【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 ...

  2. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  3. Roman to Integer && Integer to Roman 解答

    Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...

  4. Roman to Integer & Integer to Roman

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  5. Java基础之引用(String,char[],Integer)总结

    1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer  ...

  6. java中的BigDecimal和String的相互转换,int和String的类型转换,Integer类和String相互转换

    一: /*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ 注:BigDecimal在数据库中存的是number类型. import java.math.B ...

  7. 批量删除以及将String数组转换成Integer数组的奇淫技巧

    首先在pom.xml文件添加依赖: <!-- bean工具 --> <dependency> <groupId>commons-beanutils</grou ...

  8. Java基础之引用(String,char[],Integer)总结于牛客网的专项练习题

    1.String的引用: 下列代码执行后的结果为: public class Test { public static void main(String[] args) { StringBuffer ...

  9. String,int,Integer之间的转换

    public class Test{ public static void main(String[] args) { //int转换成Integer Integer in = new Integer ...

随机推荐

  1. nth-child和nth-of-type的区别

    以前一般都用:nth-child,后来知道了:nth-of-type,然后就一般用nth-of-type 它们两有什么区别呢? 首先来看个现象: 假如有这样一个HTML结构 <div class ...

  2. Android常用组件Broadcast介绍

    一.Broadcast简介 Broadcast是Android的四大组件之一.可分为: 1.普通广播 发送一个广播,所有监听该广播的广播接收者都可以监听到改广播. 2.异步广播 当处理完之后的Inte ...

  3. 从远程oracle上导入到本地同一张表中不存在的记录的方法

    场景:在远程oracle上存在一张表A,在本地同样存在一张相同表结构的表B.由于本地表B中保存了业务系统操作产生的几条记录,同时原来导入了A中的部分记录,但是并没有保存A中全部的记录.A中有15条记录 ...

  4. 【ecos学习1】wmware运行redboot[方法一]--脚本实现配置

    背景: 远程服务器Ubuntu生成软盘镜像,通过Mac下wmware运行. 1- 环境及版本: uname -a 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 ...

  5. hdu 4906 3-idiots fft

    题目链接 n个火柴棍取3个, 问能组成三角形的概率是多少. kuangbin大神的博客写的很详细了..http://www.cnblogs.com/kuangbin/archive/2013/07/2 ...

  6. xml文件的解析

    1. xml文件的解析 void CDataMgr::readStringData() { std::string xml_name = "config/string.xml"; ...

  7. python成长之路第三篇(2)_正则表达式

    打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.什么是正则表达式,python中得正则简介 2.re模块的内容 3.小练习 一.什么是正则表达式(re) 正则表 ...

  8. 补丁惹的祸-ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService

    未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出 问题: 重新安装了VS2012,结 ...

  9. Git学习03 --远程仓库

    把本地库的内容推送到远程(github), 用git push命令,实际上是把当前分支master推送到远程. 由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的 ...

  10. 使用TypeScript开发一个在线记事本,支持离线存储

    先贴上源码传送门: https://github.com/flowforever/yaryin.note 记事本网址: http://yindoc.com , 井号后面写你喜欢的文件名即可. 最近在研 ...