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 效果: ...
随机推荐
- 害死人不偿命的(3n+1)猜想 (15)
#include <iostream> #include <algorithm> using namespace std; int main(){ int n; while ( ...
- WOW.js 动画使用
有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的页面也更加有趣,那么你可以试试 WOW.js ...
- Analyzing Polyline CodeForces - 195D
Analyzing Polyline CodeForces - 195D 题意:有n个函数,第i个函数yi(x)=max(ki*x+bi,0).定义函数s(x)=y1(x)+y2(x)+...+yn( ...
- 组合数+容斥原理 UVALive 7040 Color(14西安F)
题目传送门 题意:n盆花涂色,相邻不能涂相同的颜色,从m中颜色选取k种颜色涂,保证正好有k种颜色 分析:从m中颜色选取k种就是C (m, k),然后第一个有k种选择,之后的都有k-1种选择,这样是不超 ...
- adb logcat教程
1.速查 $adb logcat -g //打印和缓冲区使用情况 $adb logcat -c main //清除main缓存区 $adb logcat -v threadtime -f /data/ ...
- HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
http://acm.hdu.edu.cn/showproblem.php?pid=5808 用bitset<120>dp,表示dp[0] = true,表示0出现过,dp[100] = ...
- Web API 实体显示注释
我看园子里关于Web API的注释都是关于方法的,并没有显示实体注释的方法,今天花了一些时间搞了一下 其实注释的显示就是根据类库的XML文档文件生成的. 首先你要将所用到的类库生成XML文档文件: 在 ...
- 801硬件检测工具DragonHD的使用
801硬件检测工具DragonHD的使用 2018/11/28 13:39 版本:V1.0 开发板:SC3817R 1.客户要认证器件,使用了全志官方的工具:DragonHD.exe 打开之后可以见用 ...
- myeclipse 安装svn(subeclipsesite)插件
(1)到官网下载subeclipsesite,下载最新的版本:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=224 ...
- java读取大文件 超大文件的几种方法
java 读取一个巨大的文本文件既能保证内存不溢出又能保证性能 import java.io.BufferedReader; import java.io.File; import jav ...