LeetCode12 Integer to Roman
题意:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析:
题目其实不难,但是需要先了解罗马数字的组成规则。
摘录百度百科如下:
- 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
- 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
- 小的数字(限于 Ⅰ、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的更多相关文章
- 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 ,即为两个并 ...
- 【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】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【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 ...
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- 5.Integer to Roman && Roman to Integer
Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
随机推荐
- [WinForm] 使用 WebBrowser 操作 HTML 頁面的 Element-摘自网络
前言 在 Window Form 應用程式如果需要瀏覽網頁時可以崁入 WebBrowser 控制項,但如果需要操作崁入的 HTML 的網頁元素,就需要額外的操作,以下紀錄幾種操作 HTML 元素的方法 ...
- 转】Mahout学习路线图
原博文出自于: http://blog.fens.me/hadoop-mahout-roadmap/ 感谢! Mahout学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目 ...
- Spark相比Hadoop MapReduce的特点
(1)中间结果输出 基于MapReduce的计算引擎通常会将中间结果输出到磁盘上,进行存储和容错. 出于任务管道承接的考虑,当一些查询翻译到MapReduce任务时,往往会产生多个Stage, ...
- UTC+0800是什么意思
<%@ language="javascript" %> <html> <body> <% var d=new Date() var h= ...
- [原创]Devexpress XtraReports 系列 6 创建并排报表
昨天我们已经介绍了如何创建交叉报表,详见:[原创]Devexpress XtraReports 系列 5 创建交叉报表 今天我们继续我们的XtraReports系列.Demo和数据库文件最后会附上. ...
- SPI移位寄存器
spi移位寄存器即是spi的数据寄存器,在stm32中数据手册是这样描述的:
- C++拓扑排序
安利一篇比较写的比较好的的博客... 拓扑排序的原理及其实现 我本来以为我看懂了原理就会打了,没想到因为没有手动实践过...原理实际上也没记清楚.... 一题HDU的拓扑裸题HDU 3342 我的拓扑 ...
- hdoj 5392 Infoplane in Tina Town
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5392 #include<stdio.h> #include<cstring> ...
- 操作Cookie的一个陷阱服务器端获取不了maxAge或其它属性
搞了几天终于弄明白了这个问题: 在读取Cookie,然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的:cookie.getMaxAge();=== ...
- MenuStrip菜单递归
C# TreeView菜单,MenuStrip菜单递归动态生成例子 http://www.cnblogs.com/ajiaojiao0303/articles/1884772.html http:// ...