LeetCode_Integer to Roman
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的更多相关文章
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- [Leetcode] Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 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 ...
- 【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 ...
- No.013:Roman to Integer
问题: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【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 ...
随机推荐
- keil 中用函数指针调用函数的参数限制
NSIC中,通过函数指针调用的函数的参数的个数没有限制,但是KeilC对此有限制,至多3个参数.因为,KeilC编译时,无法通过函数指针找到该函数的局部数据段,也就无法通过局部数据段传递参数,只能通过 ...
- Windows 1252和ISO 8859-1之间的区别(ISO 8859-1就是Latin-1,但1252与Latin1略有不同)
2.6.5. ANSI字符编码和Windows 1252 Windows为了支持英语和西欧字符,自己设计了一个编码,对应的在Code Page号是1252,被称为Windows 1252. Windo ...
- lazy load 图片延迟加载 跟随滚动条
http://plugins.jquery.com/lazyload/ Jquery.LazyLoad.js插件参数详解: 1,用图片提前占位 placeholder : "img/grey ...
- Java通过JNI调用C/C++
From:http://www.cnblogs.com/mandroid/archive/2011/06/15/2081093.html JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与 ...
- Unity PlayerPrefs类进行扩展(整个对象进行保存)
盘子脸在制作单机游戏的时候,先以为没有好多数据需要保存本地. 就没有使用json等格式自己进行保存. 使用PlayerPrefs类,但是后面字段越来越多的时候. PlayerPrefs保存就发现要手动 ...
- python list 中找连续的数字(由网友处学习)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #学习这个要求的:http://wsky.org/archives/ ...
- handsontable插件HOOK事件
Hook插件 afterChange (changes: Array, source: String):1个或多个单元格的值被改变后调用 changes:是一个2维数组包含row,prop,o ...
- ASP.net ListItem Attributes 属性回传丢失的解决方案
该方法为网上整理 1. 新继承一个列表控件 新控件中重写两个方法: using System; using System.Collections.Generic; using System.Linq; ...
- Find the maximum(规律,大数)
Find the maximum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- CREATE PROCEDURE
1 CREATE PROCEDURE(创建) CREATE PROCEDURE存储过程名(參数列表) BEGIN SQL语句代码块 END 注意: 由括号包围的參数列必须总是存在.假设没有參数,也该使 ...