您好,登錄后才能下訂單哦!
12. Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
題目大意:
將一個給定的阿拉伯?dāng)?shù)字轉(zhuǎn)換成羅馬數(shù)字。
思路:
這題看到的時候,想的太多。
其實很簡單,將千位,百位,十位,個位都表示出來,然后組合即可。
代碼如下:
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é):
有時候題目沒有那么難,不要自己搞的很復(fù)雜。問題簡單化。簡單化。。。
2016-08-19 15:16:29
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。