题目

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. 《算法竞赛进阶指南》1.6Trie

    142. 前缀统计 给定N个字符串S1,S2-SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前缀. 输入字符串的总长度不超过106,仅包含小写字母. 输入格式 ...

  2. docker监控之cadvisor

    docker run -d \ --volume=/:/rootfs:ro \ --volume=/var/run:/var/run:rw \ --volume=/sys:/sys:ro \ --vo ...

  3. Mysql的查询语句(联合查询、连接查询、子查询等)

    Mysql的各个查询语句(联合查询.连接查询.子查询等) 一.联合查询 关键字:union 语法形式 select语句1 union[union选项] select 语句2 union[union选项 ...

  4. Android-apk文件反编译

    一:工具介绍及下载 1:apktool         作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar       作用:将apk反编译成java源码(classes ...

  5. 如何改变CSV文件的编码

    通常我.csv文件的编码都不是我们想要的,比如我要把他保存为Utf-8格式的,好让我可以导入数据库,不乱码 工具/原料电脑,.csv文件方法/步骤1首先,将.csv文件保存一下.然后鼠标右击打开方式记 ...

  6. Linux下cpu过高问题排查

    原文地址:https://blog.csdn.net/chenjunan888/article/details/80447800 在服务器报cpu过高时,可使用以下命令,快速导出堆栈信息,以方便查看具 ...

  7. [未读]JavaScript高效图形编程

    去年买来就一直搁置,因为是js游戏相关,暂时还用不到.

  8. 看Facebook是如何优化React Native性能

    原文出处: facebook   译文出处:@Siva海浪高 该文章翻译自Facebook官方博客,传送门 React Native 允许我们运用 React 和 Relay 提供的声明式的编程模型, ...

  9. 动手实现 React-redux(三):connect 和 mapStateToProps

    我们来观察一下刚写下的这几个组件,可以轻易地发现它们有两个重大的问题: 有大量重复的逻辑:它们基本的逻辑都是,取出 context,取出里面的 store,然后用里面的状态设置自己的状态,这些代码逻辑 ...

  10. H+后台主题UI框架---整理(二)

    本篇文章是针对H+后台主题UI框架的整理的第二部分.主要只有一个point.如下: 其代码如下: <div class="ibox float-e-margins"> ...