Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

  分析:

罗马数字是由字符I,V,X,L,C,D,M等等表示的,其中
I = ;
V = ;
X = ;
L = ;
C = ;
D = ;
M = ;
接下来应该是V开始的重复,但是上面要加一个横线,表示对应数字的1000倍。
而且对于某位上(以个位为例), – ,应该是:I,II,III,IV,V,VI,VII,VIII,IX
而,对于百位上, – ,应该是:C,CC,CCC,CD,D,DC,DCC,DCCC,CM
依此类推。
有一点比较有意思,那就是IV,正统的写法是IIII(写程序倒是容易些)。后来简写为IV,但是由于IV也是朱皮特神的名字,为了不让神的名字看起来像普通数字,这种简写遭到了很多人的抵制。
class Solution {
public:
void appendNumtoRoman(int digit, string &roman, char *symbol)
{
if(digit == ) return ;
if(digit <= ){
roman.append(digit, symbol[]);
}else if( digit == ){
roman.append(, symbol[]);
roman.append(, symbol[]);
}else if(digit == ){
roman.append(,symbol[]);
}else if(digit <= ){
roman.append(, symbol[]);
roman.append(digit - , symbol[]);
}else if(digit == ){
roman.append(, symbol[]);
roman.append(, symbol[]);
}
}
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
char symbol[]={'I','V','X','L','C','D','M','v','x'};
string roman = "";
int scale = ; for(int i = ; i >= ; i -= )
{
int digit = num /scale ;
appendNumtoRoman(digit, roman, symbol+i);
num = num% scale;
scale /= ;
} return roman ;
}
};

转自: http://blog.unieagle.net/2012/09/29/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Ainteger-to-roman/

LeetCode_Integer to Roman的更多相关文章

  1. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  3. 【leetcode】Roman to Integer

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

  4. [Leetcode] Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. Integer to Roman -- LeetCode 012

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  6. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. No.013:Roman to Integer

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

  8. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  9. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. 一些80C51单片机支持双DPTR,C编译器是如何使用它的?

    在C51中,C编译器并不利用双DPTR来优化用户所写的程序,只有一些库例程使用了双数据指针.当在两个存储器块之间进行数据复制或比较时,以下库例程会使用双数据指针: memmovememcpymemcm ...

  2. qt之窗口换肤(一个qss的坑:当类属性发现变化时需要重置qss,使用rcc资源文件)

    1.相关文章 Qt 资源系统qt的moc,uic,rcc命令的使用 2.概要    毕业两年了,一直使用的是qt界面库来开发程序,使用过vs08.10.13等开发工具,并安装了qt的插件,最近在做客户 ...

  3. 地址重写--Java中urlrewriter的使用

    最近公司以前的一个项目需要升级改版,其中的一个模块是使用Struts2做的不需要改动,但是需要将其从之前的项目里面剥离出来,看了看官网,发现所有的链接访问的静态地址,以为是FreeMarker实现的, ...

  4. ThreadPoolExecutor参数解析

    ThreadPoolExecutor是一个非常重要的类,用来构建带有线程池的任务执行器,通过配置不同的参数来构造具有不同规格线程池的任务执行器. 写在前面的是: 线程池和任务执行器,线程池的定义比较直 ...

  5. unix c 08

    信号 - signal()改变信号的处理方式.默认情况下,信号在Unix中都有自己的处理方式,如果想改变信号的处理方式,signal/sigaction 可以实现. 信号可以在关键代码处进行屏蔽,因为 ...

  6. DELL WIN7系统安装 U盘

    1.老毛挑制作U盘启动安装  http://www.laomaotao.net/ 2.下载WIN7 COPY里面的内容到U盘根目录,然后将bootmgr文件更名为win7mgr 3.开进F2修改BIO ...

  7. poj 1503 大数相加(java)

    代码: import java.math.*; import java.util.Scanner; public class Main { public static void main(String ...

  8. python高级编程之(类级):子类内建类型

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #类级 #在2.2中,提出了类型(type0与类(class)统一( ...

  9. 集成支付宝SDK遇到的坑

    一.首先我先把集成过程说一下.小编想说的话:支付宝是我做支付中觉得坑最多的一个,各种编译不过,各种出问题. 废话不多说,进入主题:1.首先当前是下载官方SDK啦,当前你也可以通过cocopods进行导 ...

  10. IOS 判断设备类型

    - (NSString*)deviceString { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname( ...