class Solution { public: string intToRoman(int num) { if( num == 0 ) return "0"; string romanSymbol[4][10] = { {"0", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, {"0", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, {"0", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, {"0", "M", "MM", "MMM"} }; int romanValue[4] = { 1, 10, 100, 1000 }; string romanStr; int tmpN = num; int digit = 3; int idx; while(tmpN > 0) { idx = tmpN / romanValue[digit]; if(idx > 0) romanStr += romanSymbol[digit][idx]; tmpN = tmpN % romanValue[digit]; digit--; } return romanStr; } };
程式設計可以改變您的未來(Programming can change your future)。 雲林SONG 全名為雲林軟體工程(SOftware eNGineering),目標致力於軟體人才的培養並推廣開源軟體落實於資訊教育。程式設計的觀念是軟體產品的基礎,程式碼就像沙子一樣,要紮實,所建立出來的高塔才會穩固。本站也提供資訊教育相關的教學資源。 YunlinSONG stands for Yunlin SOftware eNGineering, offering tutorial for computer programming and promoting open-source software. Teaching resources in information technology education are provided here.
▼
LeetCode OJ: 12. Integer to Roman 十進制整數轉成羅馬數字
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
題目連結,此題跟LintCode: Roman to Integer 羅馬數字轉成十進制整數剛好相反,直接用查表法解:
沒有留言:
張貼留言