Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解题:
参考别人的代码!
Code:
class Solution {
public:
string intToRoman(int num)
{
string ret;
int number=num;
map<int,string> vmap;
vmap.insert(make_pair(1,"I"));
vmap[4]="IV";
vmap[5]="V";
vmap[9]="IX";
vmap[10]="X";
vmap[40]="XL";
vmap[50]="L";
vmap[90]="XC";
vmap[100]="C";
vmap[400]="CD";
vmap[500]="D";
vmap[900]="CM";
vmap[1000]="M";
for(map<int,string>::reverse_iterator it=vmap.rbegin();it!=vmap.rend();it++)
{
while(number>=it->first)
{
number-=it->first;
ret+=it->second;
}
}
return ret;
}
};