lintcode :Integer to Roman 整数转罗马数字
生活随笔
收集整理的這篇文章主要介紹了
lintcode :Integer to Roman 整数转罗马数字
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目
?整數(shù)轉(zhuǎn)羅馬數(shù)字?
給定一個(gè)整數(shù),將其轉(zhuǎn)換成羅馬數(shù)字。
返回的結(jié)果要求在1-3999的范圍內(nèi)。
樣例4?->?IV
12?->?XII
21?->?XXI
99?->?XCIX
更多案例,請(qǐng)戳?http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
說(shuō)明什么是?羅馬數(shù)字?
- https://en.wikipedia.org/wiki/Roman_numerals
- https://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97
- http://baike.baidu.com/view/42061.htm
解題
?拼寫規(guī)則
羅馬數(shù)字共有7個(gè),即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的規(guī)則可以表示任意正整數(shù)。需要注意的是羅馬數(shù)字中沒有“0”,與進(jìn)位制無(wú)關(guān)。一般認(rèn)為羅馬數(shù)字只用來(lái)記數(shù),而不作演算。重復(fù)數(shù)次:一個(gè)羅馬數(shù)字重復(fù)幾次,就表示這個(gè)數(shù)的幾倍。
右加左減:
在較大的羅馬數(shù)字的右邊記上較小的羅馬數(shù)字,表示大數(shù)字加小數(shù)字。
在較大的羅馬數(shù)字的左邊記上較小的羅馬數(shù)字,表示大數(shù)字減小數(shù)字。
左減的數(shù)字有限制,僅限于I、X、C。比如45不可以寫成VL,只能是XLV
但是,左減時(shí)不可跨越一個(gè)位數(shù)。比如,99不可以用IC(100-1)表示,而是用XCIX(100-10 ?10 -1)表示。(等同于阿拉伯?dāng)?shù)字每位數(shù)字分別表示。)
左減數(shù)字必須為一位,比如8寫成VIII,而非IIX。
右加數(shù)字不可連續(xù)超過(guò)三位,比如14寫成XIV,而非XIIII。?
加線乘千:
在羅馬數(shù)字的上方加上一條橫線或者加上下標(biāo)的?,表示將這個(gè)數(shù)乘以1000,即是原數(shù)的1000倍。
同理,如果上方有兩條橫線,即是原數(shù)的1000000?倍。
數(shù)碼限制:
同一數(shù)碼最多只能連續(xù)出現(xiàn)三次,如40不可表示為XXXX,而要表示為XL。
例外:由于IV是古羅馬神話主神朱庇特(即IVPITER,古羅馬字母里沒有J和U)的首字,因此有時(shí)用IIII代替IV。? 根據(jù)上面規(guī)則,為了防止出現(xiàn)高于三次的字符,對(duì)高于三次的基本字符進(jìn)行先處理
| I | IV | IX | X | XL | L | XC | C | CD | D | CM | M |
| 1 | 4 | 5 | 9 | 10 | 40 | 50 | 90 | 100 | 500 | 900 | 1000 |
這樣對(duì)任一個(gè)羅馬數(shù)字可以 由上面的12個(gè)字符進(jìn)行加法操作完成,即:大數(shù)在左,小數(shù)在右。《具體為什么,我不是很理解,類似于人民幣只有:100,50,20,10,5,2,1,0.5,0.2,0.1可以組成任意金額》
下面程序雖然很差,但是很好理解
public class Solution {/*** @param n The integer* @return Roman representation*/public String intToRoman(int n) {// Write your code hereif(n<1 || n>3999)return "ERROR";String s="";while(n>=1000){s += "M";n -= 1000;}while(n >= 900){s += "CM";n -= 900;}while( n>= 500){s += "D";n -= 500;}while( n>= 400){s += "CD";n -= 400;}while( n >= 100){s += "C";n -= 100;}while( n>= 90){s += "XC";n -= 90;}while( n>= 50){s += "L";n -= 50;}while( n >= 40){s += "XL";n -= 40;}while( n>= 10){s += "X";n -= 10;}while( n>= 9){s +="IX";n -= 9;}while( n>=5 ){s += "V";n -= 5;}while( n >= 4 ){s +="IV";n -= 4;}while(n >= 1 ){s +="I";n -= 1;}return s;} } Java Code然后可以改寫成下面的形式
public class Solution {/*** @param n The integer* @return Roman representation*/public String intToRoman(int n) {// Write your code hereint[] numbers = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };String[] letters = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };String res ="" ;for(int i = 0;i<13;i++){if(n >= numbers[i]){int count = n/numbers[i];n = n%numbers[i];for(int j=0;j<count ;j++){res= res + letters[i];}}}return res; } } Java CodePython程序也是很簡(jiǎn)單
class Solution:# @param {int} n The integer# @return {string} Roman representationdef intToRoman(self, n):# Write your code herenumbers = [1000,900,500,400,100,90,50,40,10,9,5,4,1]letters = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]res =[]for i in range(13):if n>= numbers[i]:count = n/numbers[i]n %= numbers[i]res += letters[i]*countreturn "".join(res) Python Code參考鏈接
轉(zhuǎn)載于:https://www.cnblogs.com/theskulls/p/4957173.html
總結(jié)
以上是生活随笔為你收集整理的lintcode :Integer to Roman 整数转罗马数字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中国和韩国一致公认刘亦菲是古装第一美女,
- 下一篇: 不孕不育有得治吗