题意:

Given an integer, convert it to a roman numeral.

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

分析:

题目其实不难,但是需要先了解罗马数字的组成规则。

摘录百度百科如下:

  1. 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
  2. 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
  3. 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9

所以打表存一下数字和罗马字母的对应关系,从千位开始向下处理即可。

代码:

 class Solution {
public:
string intToRoman(int num) {
string flag[][] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
string result;
int mod1 = ;
int sum = ;
while (mod1 != ) {
int temp = num / mod1;
result += flag[sum][temp];
num -= temp * mod1;
sum--;
mod1 /= ;
}
return result;
}
};

LeetCode12 Integer to Roman的更多相关文章

  1. Leetcode12.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 ,即为两个并 ...

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

  3. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  4. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  5. [LintCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...

  6. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  7. LeetCode:Roman to Integer,Integer to Roman

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

  8. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  9. No.012 Integer to Roman

    12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...

随机推荐

  1. cocos2d-html5对话界面设计

    "use strict" var _dialogMsgBottom = null; var _dialogMsgTop = null; ; var _dialogConfig = ...

  2. Netbeans Platform 工程,免安装JDK

    使用Netbeans 6.8 创建了一个Netbeans Platform 工程,以Zip形式发布后, 按照以下操作,可 以在客户端免安装JDK: 1. 从已安装JDK的计算机中,提取JDK:eg. ...

  3. Linux下的cut选取命令详解

    定义 正如其名,cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的.cut是以每一行为一个处理对象的,这种机制和sed是一样的 剪切依据 cut命令主要是接受三个定位方法: 第一,字节(by ...

  4. What's Exposure?

    [What's Exposure?] ISO:即相机的感光度.ISO数值的大小是DC对光线反应的敏感程度测量值,通常以ISO数值表示,数值越大表示对光线的敏感性越强,数值越小表示越弱,是控制曝光量的一 ...

  5. LightOJ 1282 Leading and Trailing (快数幂 + 数学)

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  6. Java的位运算符—— 与(&)、非(~)、或(|)、异或(^)

    位运算符主要针对二进制,它包括了:“与”.“非”.“或”.“异或”.从表面上看似乎有点像逻辑运算符,但逻辑运算符是针对两个关系运算符来进行逻辑运算,而位运算符主要针对两个二进制数的位进行逻辑运算.下面 ...

  7. mysql show命令集合

    a. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称b. show databases; -- 显示mysql中所有数据库的 ...

  8. web.xml配置详解之listener与context-param

    1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context- ...

  9. Oracle数据库定时任务配置和日志执行情况查询

    基础配置: /***************************************************************** * * 移动抵扣券快到期推送提醒 * 首次执行 : 2 ...

  10. Form实现主从块金额汇总

    1.FORM使用app_calculate.running_total汇总行金额,行上有编码重复验证. 情况一:当录入多个编码重复的行并保存时,报错,清除一个重复行再保存(头行金额一致),报错&quo ...