题目

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;
}
}
};

Git测试程序代码

LeetCode(12)Integer to Roman的更多相关文章

  1. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  2. 【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 ...

  3. leetcode第12题--Integer to Roman

    Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  4. LeetCode(12):整数转罗马数字

    Medium! 题目描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 ...

  5. HD-ACM算法专攻系列(12)——Integer Inquiry

    问题描述: 源码: import java.math.BigInteger; import java.util.*; public class Main { //主函数 public static v ...

  6. leecode刷题(12)-- 整数反转

    leecode刷题(12)-- 整数反转 整数反转 描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: - ...

  7. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

  8. Leetcode(2)两数相加

    Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两 ...

  9. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

随机推荐

  1. UltraEdit - 怎么显示文件标签栏和侧边栏

    显示文件标签栏 view -> views/lists -> open Files Tabs 显示侧边栏 view -> views/lists -> File Tree Vi ...

  2. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A

    Description Your search for Heidi is over – you finally found her at a library, dressed up as a huma ...

  3. The Specials Menu LightOJ - 1025

    The Specials Menu LightOJ - 1025 题意:在给定的字符串中删去一些字符,使其成为回文串(不能全部都删).求方案数. 方法:常规的区间dp.ans[i][j]表示在i到j的 ...

  4. Eclipse安装jad反编译插件(在线安装)

    Help→Eclipse Marketplace→Find→jad 然后等安装完成重启eclipse即可

  5. MySQL GTID复制

    什么是GTID 什么是GTID呢, 简而言之,就是全局事务ID(global transaction identifier ),最初由google实现,官方MySQL在5.6才加入该功能.GTID是事 ...

  6. python_函数嵌套(4)

    第1章 名称空间 1.1 定义 1.2 变量运行流程 1.3 临时名称空间 1.4 python三种名称空间 第2章 作用域 2.1 作用域分类 2.2 加载顺序 2.3 取值顺序 函数嵌套 2.4 ...

  7. PHP的扩展知识

    1. 图片的上传大小受哪一些限制? 答:浏览器 其实php.ini文件里面 1. upload_max_filesize = 2M  限制单个文件上传大小 2. post_max_size = 8M ...

  8. hihocoder offer收割编程练习赛8 C 数组分拆

    思路:(引自bfsoyc的回答:http://hihocoder.com/discuss/question/4160) 动态规划.状态dp[i]表示 前i个数的合法的方案数,转移是 dp[i] = s ...

  9. ES6—带默认值的函数参数及其作用域

    在学习ES6函数一章时,发现了一个有意思的现象,原文描述如下: 这段话主要state了3个事实: ①函数参数有默认值时,会在声明初始化阶段形成一个单独的作用域 ②这个作用域在初始化结束后消失 ③没默认 ...

  10. grep的几个参数

    -a 在二进制问就爱你中,以文本方式进行搜索 -c 计算找到搜索字符串的次数 -i 忽略大小写 -n 输出行号 -v 反向选择,即没有显示搜索字符串内容的那一行 grep -n '\.$'  file ...