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 ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
- I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
- X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
- C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
示例 1:
输入: 3 输出: "III"
示例 2:
输入: 4 输出: "IV"
示例 3:
输入: 9 输出: "IX"
示例 4:
输入: 58 输出: "LVIII" 解释: L = 50, V = 5, III = 3.
示例 5:
输入: 1994 输出: "MCMXCIV" 解释: M = 1000, CM = 900, XC = 90, IV = 4.
class Solution {
public:
string intToRoman(int num)
{
string res = "";
if(num / 1000 != 0)
{
int temp = (num / 1000);
num = num % 1000;
for(int i = 0; i < temp; i++)
res += 'M';
}
if(num / 500 != 0)
{
if(num >= 900)
{
res += "CM";
num -= 900;
}
else
{
res += 'D';
num -= 500;
int temp = (num / 100);
for(int i = 0; i < temp; i++)
res += 'C';
num = num % 100;
}
}
if(num / 100 != 0)
{
if(num >= 400)
{
res += "CD";
num -= 400;
}
else
{
int temp = (num / 100);
for(int i = 0; i < temp; i++)
res += 'C';
num = num % 100;
}
}
if(num / 50 != 0)
{
if(num >= 90)
{
res += "XC";
num -= 90;
}
else
{
res += 'L';
num -= 50;
int temp = (num / 10);
for(int i = 0; i < temp; i++)
res += 'X';
num = num % 10;
}
}
if(num / 10 != 0)
{
if(num >= 40)
{
res += "XL";
num -= 40;
}
else
{
int temp = (num / 10);
for(int i = 0; i < temp; i++)
res += 'X';
num = num % 10;
}
}
if(num / 5 != 0)
{
if(num >= 9)
{
res += "IX";
num -= 9;
}
else
{
res += 'V';
num -= 5;
int temp = (num / 1);
for(int i = 0; i < temp; i++)
res += 'I';
num = num % 1;
}
}
if(num / 1 != 0)
{
if(num >= 4)
{
res += "IV";
num -= 4;
}
else
{
int temp = (num / 1);
for(int i = 0; i < temp; i++)
res += 'I';
num = num % 1;
}
}
return res;
}
};
Leetcode12.Integer to Roman整数转罗马数字的更多相关文章
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 【LeetCode】Integer to Roman(整数转罗马数字)
这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- Leetcode12--->Integer to Roman(整数转换为罗马数字)
题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- light oj 1231 dp 多重背包
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- mysql插入数据显示:Incorrect datetime value: '0000-00-00 00:00:00'
1. 在进行mysql数据插入的时候,由于mysql的版本为5.7.1,部分功能已经升级,导致在datetime数据类型的影响下出现错误: 数据插入: mysql>insert into j ...
- Android基础控件RatingBar星级评分条的使用
1.简介 RatingBar继承ProgressBar,除了ProgressBar的属性外还有特有属性: android:isIndicator:是否用作指示,用户无法更改,默认false andro ...
- disruptor 高效队列
disruptor 是什么: disruptor 是一个 低延时的 无锁 环形 队列. 相较于 java的 队列 ,他有明显的优点 ,无界,无锁,低延时(解决了为内存共享问题 ) disrupto ...
- day 39 MySQL之多表查询
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都 ...
- Django logging 的配置方法
做开发离不开日志,以下是我在工作中写Django项目常用的logging配置. BASE_LOG_DIR = os.path.join(BASE_DIR, "log") LOGGI ...
- vue elment.style样式修改(第三方组件自生成元素)
参考:https://blog.csdn.net/dcxia89/article/details/80402490 https://blog.csdn.net/jianglibo102 ...
- Activiti流程定义部署、删除
1.部署流程定义 部署流程定义也可以认为是增加流程定义. 首先创建流程引擎对象(公用的方法) private ProcessEngine processEngine = ProcessEngin ...
- 记录下sparkStream的做法(scala)
一直用storm做实时流的开发,之前系统学过spark但是一直没做个模版出来用,国庆节有时间准备做个sparkStream的模板用来防止以后公司要用.(功能模拟华为日常需求,db入库hadoop环境) ...
- Linux C类型
BOOL: http://www.cnblogs.com/pharen/archive/2012/02/06/2340257.html GCC编译器参数介绍: http://blog.csdn.net ...