题目
分析
这里我们将所有特殊的情况全部加入到哈希表中
代码实现
public static String intToRoman(int num) { StringBuilder sb=new StringBuilder(); String[][] map={{"1000","M"},{"900","CM"},{"500","D"},{"400","CD"},{"100","C"}, {"90","XC"},{"50","L"},{"40","XL"},{"10","X"},{"9","IX"}, {"5","V"},{"4","IV"}, {"1","I"}}; for(int i=0;i<map.length;i++) { int cur=Integer.parseInt(map[i][0]); if(num>=cur) { int count=num/cur; num=num%cur; for(int j=0;j<count;j++) { sb.append(map[i][1]); } } } return sb.toString(); }
完成情况
1次