请将给出的整数转化为罗马数字
保证输入数字的范围在1 到 3999之间。
示例1 输入 1 输出 "I"
首先搞清楚罗马数字,然后枚举出符号
I IV V IX X XL L XC C CD D CM M
1 4 5 9 10 40 50 90 100 400 500 900 1000
从后往前逐个比对就可以了
public String intToRoman (int num) { StringBuffer sb = new StringBuffer(); int[] s = new int[]{1,4,5,9,10,40,50,90,100,400,500,900,1000}; String[] st=new String[]{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; for(int i =s.length-1;i>=0;){ if(num>=s[i]){ sb=sb.append(st[i]); num-=s[i]; }else{ i--; } } return sb.toString(); }