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 ...
随机推荐
- React中的this.props.children
React this.props.children this.props对象的属性与组件的属性一一对应,但是有一个例外,就是this.props.children属性.它表示组件的所有子节点. var ...
- light oj 1231 dp 多重背包
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- Template-Thymeleaf:Thymeleaf
ylbtech-Template-Thymeleaf:Thymeleaf 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 0. https://www.thyme ...
- iOS开发本地推送
1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...
- PageRank算法R语言实现
PageRank算法R语言实现 Google搜索,早已成为我每天必用的工具,无数次惊叹它搜索结果的准确性.同时,我也在做Google的SEO,推广自己的博客.经过几个月尝试,我的博客PR到2了,外链也 ...
- python 拷贝某个文件到另一个目录下
python的shutil包含有很多文件拷贝的函数,各种各样的,要实现我文章题目的目的,使用shutil.copy函数即可 shutil.copy(文件的路径,另一个目录)
- java虚拟机(十)--性能监控工具测试内存溢出和死锁基本思路
在之前就曾经简单介绍过jdk自带的性能检测工具,但是只是很入门的内容.没有真正的用过都是白扯了,面试的时候也说不过去,更别提真正 在生产环境去解决问题,所以这里我们学习一下真正解决问题的过程,最起码面 ...
- csp-s模拟47 Emotional Flutter,Endless Fantasy题解
题面:https://www.cnblogs.com/Juve/articles/11558523.html A:Emotional Flutter 如果起点确定,那么我们后面走的点都是固定的,及mo ...
- js中call、apply、bind的区别和应用
一.总体概述 这三个方法都是改变函数本身的this指向的,但是略有不同 1.call 使用方法:函数名.call(this指向,展开的参数列表); 如果传入的this指向是null或者是undifin ...
- Vue的Ajax(vue-resource/axios)
一 简介 1.vue本身不支持发送AJAX请求,需要使用vue-resource(vue1.0版本).axios(vue2.0版本)等插件实现 2.axios是一个基于Promise的HTTP请求客户 ...