【leetcode】Integer to Roman
生活随笔
收集整理的這篇文章主要介紹了
【leetcode】Integer to Roman
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近使用開發(fā)的過程中出現(xiàn)了一個(gè)小問題,順便記錄一下原因和方法--
????Question :?
????Given an integer, convert it to a roman numeral.
????Input is guaranteed to be within the range from 1 to 3999.
????Anwser 1 : ? ?
class Solution { public:string intToRoman(int num) {// Start typing your C/C++ solution below// DO NOT write int main() function string res;string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};int value[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};int i = 0;while(num != 0){if(num >= value[i]){ // minus largest numbernum -= value[i];res += symbol[i];} else {i++; }}return res;} };????Anwser 2 : ??
每日一道理古人云:“海納百川,有容乃大。”人世間,不可能沒有矛盾和爭吵,我們要以磊落的胸懷和寬容的微笑去面對(duì)它 。哈伯德也曾說過:“寬恕和受寬恕的難以言喻的快樂,是連神明都會(huì)為之羨慕的極大樂事。”讓我們從寬容中享受快樂,從諒解中體會(huì)幸福吧!
class Solution { public:string intToRoman(int num) {// Start typing your C/C++ solution below// DO NOT write int main() function char symbol[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};string res = "";int scale = 1000;for (int i = 6; i >= 0; i -= 2){int digit = num / scale;num2roman(digit, res, symbol + i);num %= scale;scale /= 10;}return res;}void num2roman(int num, string& res, char symbols[]){if (num == 0)return;if (num <= 3)res.append(num, symbols[0]);else if (num == 4){res.append(1, symbols[0]);res.append(1, symbols[1]);}else if (num <= 8){res.append(1, symbols[1]);res.append(num - 5, symbols[0]);}else{res.append(1, symbols[0]);res.append(1, symbols[2]);}} };
文章結(jié)束給大家分享下程序員的一些笑話語錄: 一位程序員去海邊游泳,由于水性不佳,游不回岸了,于是他揮著手臂,大聲求.救:“F1,F1!”
轉(zhuǎn)載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/21/3034383.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的【leetcode】Integer to Roman的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对计算机考研的认识
- 下一篇: 感觉又学到了不少,在这里写下来,但也有一