2020--07--12 整形数字转化为罗马数
integer-to-roman
https://www.nowcoder.com/practice/0636c3db0de6437a8a86e58f46aa5c90?tpId=46&&tqId=29166&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/question-ranking
请将给出的整数转化为罗马数字
保证输入数字的范围在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(); }