12. Integer to Roman
我們提供的服務(wù)有:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、溫宿ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的溫宿網(wǎng)站制作公司
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
題目大意:
將一個(gè)給定的阿拉伯?dāng)?shù)字轉(zhuǎn)換成羅馬數(shù)字。
思路:
這題看到的時(shí)候,想的太多。
其實(shí)很簡(jiǎn)單,將千位,百位,十位,個(gè)位都表示出來(lái),然后組合即可。
代碼如下:
class Solution { public: string intToRoman(int num) { string thousands[4] = {"","M","MM","MMM"}; string hundreds[10] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; string tens[10] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; string units[10] = {"","I","II","III","IV","V","VI","VII","VIII","IX"}; string * hits[4] = {units,tens,hundreds,thousands}; string result; int index = 0; while (num > 0) { result = hits[index][num % 10] + result; num = num / 10; index++; } return result; } };
總結(jié):
有時(shí)候題目沒(méi)有那么難,不要自己搞的很復(fù)雜。問(wèn)題簡(jiǎn)單化。簡(jiǎn)單化。。。
2016-08-19 15:16:29