Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.
解题思路:直接对输入的num,直接求出各位上的数。
class Solution {
public String intToRoman(int num) {
Map<Integer,String> map = new HashMap<Integer,String>(){
{
put(0,"");
put(1,"I");put(2,"II");put(3,"III");put(4,"IV");put(5,"V");
put(6,"VI");put(7,"VII");put(8,"VIII");put(9,"IX");
put(10,"X");put(20,"XX");put(30,"XXX");put(40,"XL");put(50,"L");
put(60,"LX");put(70,"LXX");put(80,"LXXX");put(90,"XC");
put(100,"C");put(200,"CC");put(300,"CCC");put(400,"CD");put(500,"D");
put(600,"DC");put(700,"DCC");put(800,"DCCC");put(900,"CM");
put(1000,"M");put(2000,"MM");put(3000,"MMM");
}
};
int q = num/1000;
int b = num%1000/100;
int s = num%100/10;
int g = num % 10;
return map.get(q*1000)+map.get(b*100)+map.get(s*10)+map.get(g);
}
}
![](https://img-my.csdn.net/uploads/201710/13/1507899953_5710.png)