您的位置:首页 > 其它

Leetcode84: Integer to Roman

2015-09-29 10:46 274 查看
Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

struct node{
int key;
string szRoman;
node(int k, string s):key(k), szRoman(s){}
};

string intToRoman(int num){
vector<node> dct;
dct.push_back(node(1000, "M"));
dct.push_back(node(900,  "CM"));
dct.push_back(node(500,  "D"));
dct.push_back(node(400,  "CD"));
dct.push_back(node(100,  "C"));
dct.push_back(node(90,   "XC"));
dct.push_back(node(50,   "L"));
dct.push_back(node(40,   "XL"));
dct.push_back(node(10,   "X"));
dct.push_back(node(9,	"IX"));
dct.push_back(node(5,	"V"));
dct.push_back(node(4,	"IV"));
dct.push_back(node(1,	"I"));

string res;
int i = 0;
while(num > 0)
{
if(num/dct[i].key == 0)
{
i += 1;
continue;
}

for(int j = 0; j < num/dct[i].key; ++j)
res.append(dct[i].szRoman);

num%=dct[i].key;
}
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: