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. Keil C51里关于堆栈指针的处理

    Keil C是非常优秀的C51编译器,可能是最好的C51编译器,提供各种优化模式,对变量的优化和地址安排做得非常好.这是用C语言写代码的好处之一,如果用汇编写,得费一大番功夫给各个变量安排内存物理地址 ...

  2. PowerShell远程连接主机进行会话

    Get-ExecutionPolicy #脚本的执行策略set-ExecutionPolicy 枚举值 不同的策略,执行脚本的权限不同 允许开启远程 Enable-PSRemoting 添加域账户或者 ...

  3. linux xargs 使用

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题[1]. 例如,下面的命令: rm `find /path -type ...

  4. OpenWrt刷机

    http://blog.163.com/l1_jun/blog/static/14386388201441344612/ http://blog.chinaunix.net/uid-25073805- ...

  5. 把分类的select写在moden里做成一个组件 高洛峰

    function selectform($name="pid", $pid=0) { $data = $this->field('id, concat(path, " ...

  6. 详述.NET里class和struct的异同

    结构与类共享几乎所有相同的语法,但结构比类受到的限制更多:尽管结构的静态字段可以初始化,结构实例字段声明还是不能使用初始值设定项. 结构不能声明默认构造函数(没有参数的构造函数)或析构函数. 结构的副 ...

  7. Find the maximum(规律,大数)

    Find the maximum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  8. [置顶] 【cocos2d-x入门实战】微信飞机大战之十二:分数的本地存储

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/12036237 作为一个单机游戏,连分数存储的的功能都没有,让它怎么在单机游戏圈里混 ...

  9. Ubuntu12.04 Git 服务器详细配置

    Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的.今天我们一起学习Git服务器在Ubunt ...

  10. 虚拟化之docker

    1,什么是docker dokcer是应用容器,它是基于系统的虚拟化,他不是基于硬件的虚拟化(KVM),可能这样说有点抽象,我在知乎看到过一个比喻, docker思想是来源于集装箱的,在一艘大船上,可 ...