我开始想用map.lower_bound的,但是报错了,就直接暴力搜索了。
class Solution {
public:
string intToRoman(int num) {
map<int,string> mp;
mp[1] = "I";
mp[4] = "IV";
mp[5] = "V";
mp[9] = "IX";
mp[10] = "X";
mp[40] = "XL";
mp[50] = "L";
mp[90] = "XC";
mp[100] = "C";
mp[400] = "CD";
mp[500] = "D";
mp[900] = "CM";
mp[1000] = "M";
string ans = "";
while(num != 0)
{
//map<int,char>::iterator it = mp.lower_bound(num,greater<int>());
map<int,string>::iterator it;
for(map<int,string>::iterator iter = mp.begin();iter != mp.end();iter++)
{
if(iter->first > num)
{
break;
}
it = iter;
}
num -= it->first;
ans += it->second;
}
return ans;
}
};