LeetCode(12)Integer to Roman
题目
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析
该题目要求将给定的1~3999之间的整型数字转换为罗马数字并输出。
解这道题我们必须了解罗马字母与整数之间的对应:
对照举例如下:
AC代码
class Solution {
public:
string intToRoman(int num) {
//存储罗马数字
string str;
if (num == 0)
return "";
//(1)首先处理最高位千位数字
if (num >= 1000)
{
int count = num / 1000;
for (int i = 0; i < count; i++)
str += RomanLeter(1000);
//得到百位数
num %= 1000;
//链接其余三位数字对应的罗马序列
str += intToRoman(num);
}//else if
else if (num >= 100)
{
if (num >= 900)
{
str = str + RomanLeter(100) + RomanLeter(1000);
num %= 100;
}//if
else if (num >= 500)
{
str += RomanLeter(500);
num -= 500;
}//else if
else if (num >= 400){
str = str + RomanLeter(100) + RomanLeter(500);
num -= 400;
}
else{
while (num >= 100)
{
str += RomanLeter(100);
num -= 100;
}//while
}
str += intToRoman(num);
}//else if
else if (num >= 10)
{
if (num >= 90)
{
str = str + RomanLeter(10) + RomanLeter(100);
num %= 10;
}//if
else if (num >= 50)
{
str += RomanLeter(50);
num -= 50;
}
else if (num >= 40){
str = str + RomanLeter(10) + RomanLeter(50);
num -= 40;
}
else{
while (num >= 10)
{
str += RomanLeter(10);
num -= 10;
}
}
str += intToRoman(num);
}
else if (num >= 1)
{
if (num == 9)
{
str = str + RomanLeter(1) + RomanLeter(10);
num /= 10;
}
else if (num >= 5)
{
str += RomanLeter(5);
num -= 5;
}
else if (num >= 4){
str = str + RomanLeter(1) + RomanLeter(5);
num -= 4;
}
else{
while (num >= 1)
{
str += RomanLeter(1);
num -= 1;
}
}
str += intToRoman(num);
}
else
str += "\0";
return str;
}
string RomanLeter(int n)
{
switch (n)
{
case 1:
return "I"; break;
case 5:
return "V"; break;
case 10:
return "X"; break;
case 50:
return "L"; break;
case 100:
return "C"; break;
case 500:
return "D"; break;
case 1000:
return "M"; break;
default:
return ""; break;
}
}
};
LeetCode(12)Integer to Roman的更多相关文章
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- leetcode第12题--Integer to Roman
Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- LeetCode(12):整数转罗马数字
Medium! 题目描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...
- HD-ACM算法专攻系列(12)——Integer Inquiry
问题描述: 源码: import java.math.BigInteger; import java.util.*; public class Main { //主函数 public static v ...
- leecode刷题(12)-- 整数反转
leecode刷题(12)-- 整数反转 整数反转 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: - ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- Leetcode(2)两数相加
Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两 ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
随机推荐
- java实训 :异常(try-catch执行顺序与自定义异常)
关键字: try:执行可能产生异常的代码 catch:捕获异常 finally:无论是否发生异常代码总能执行 throws:声明方法可能要抛出的各种异常 throw:手动抛出自定义异常 用 try-c ...
- Codeforces Round #261 (Div. 2) D
Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...
- bzoj2154||洛谷P1829 Crash的数字表格&&JZPTAB && bzoj3309 DZY Loves Math
bzoj2154||洛谷P1829 https://www.lydsy.com/JudgeOnline/problem.php?id=2154 https://www.luogu.org/proble ...
- 线段树(区间合并) HDOJ 3308 LCIS
题目传送门 题意:线段树操作:1. 单点更新 2. 求区间的LCIS(longest consecutive increasing subsequence) 分析:注意是连续的子序列,就是简单的区间合 ...
- (转)生产者/消费者问题的多种Java实现方式
参考来源:http://blog.csdn.net/monkey_d_meng/article/details/6251879/ 生产者/消费者问题的多种Java实现方式 实质上,很多后台服务程序并发 ...
- Backbone学习记录(7)
事件委托 <form> <input type="text" class="txt"> <input type="but ...
- 基于坐标的自动化测试神器---Total Control快速入门
1.Total Control简单介绍 一款能够在PC上控制手机的软件,同时可以使用PC 触摸屏.鼠标.键盘, 全面操控 Android 手机,只需通过 USB 或 WiFi 连接手机至电脑,即可随时 ...
- hash系列集合的性能优化
hash系列的集合: HashSet.LinkedHashSet 采用hash算法决定元素在集合中的存储位置 HashMap.LinkedHashMap.Hashtable 采用hash算 ...
- android动画之android:interpolator属性使用
android动画之android:interpolator使用 Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerat ...
- java写跳一跳辅助程序
##起初是想使用按键精灵脚本程序控制,但还是选择熟悉的java.我这里使用了工具,造成延迟问题.也求教:java控制安卓的正确姿势, 参考了.NET玩跳一跳,思路都是一样的,只不过使用ADB控制安卓的 ...