题目

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. hbase-shell + hbase的java api

    本博文的主要内容有 .HBase的单机模式(1节点)安装 .HBase的单机模式(1节点)的启动 .HBase的伪分布模式(1节点)安装   .HBase的伪分布模式(1节点)的启动    .HBas ...

  2. css3 background-size属性--ie兼容

    css3 background-size属性--ie兼容 safari浏览器对background-size 属性显示原始大小 background-size: cover not working o ...

  3. codeforces 629C Famil Door and Brackets (dp + 枚举)

    题目链接: codeforces 629C Famil Door and Brackets 题目描述: 给出完整的括号序列长度n,现在给出一个序列s长度为m.枚举串p,q,使得p+s+q是合法的括号串 ...

  4. android动画(2)自定义动画

    public class CustomAnimation extends Animation { // 这个方法可以获得动画view的width,height,以及它父view的width @Over ...

  5. JavaScript入门2

    5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...

  6. Backbone学习记录(6)

    路由 backbone将路由规则和一个方法名绑定到一起,来控制单页的hash,以及单页的前进后退. var UserRouter = Backbone.Router.extend({ routes: ...

  7. servlet基础概念

    一.servlet是什么? 运行在Web服务器上(如:tomcat),作为浏览器请求与数据库或其他应用程序之间的中间层 二.servlet主要任务: 1.读取浏览器发送的显式数据(如:html表单)隐 ...

  8. 常用的HTML5 pattern属性

    type="tel" 和 type="number" 的区别 这里还是先那么先交代一下最初遇到的问题.其实无论是tel还是number都不是完美的: type= ...

  9. 复习-PEP8规范(转)

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换 ...

  10. [ POI 2017 ] Sabota?

    Description 题目链接 Solution 因为一个节点染黑了子树就都被染黑了,所以最后染黑的点集必然是一棵子树. 可以得出的结论是,如果被染黑的节点在节点 \(a\) 的子树中,而 \(a\ ...