import java.util.*; /** * NC361 整数转罗马数字 * @author d3y1 */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return string字符串 */ public String ArabicToRoman (int n) { int[] arabicArr = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] romanArr = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; StringBuilder sb = new StringBuilder(); // 贪心 int i = 0; while(n != 0){ while(i<arabicArr.length && n<arabicArr[i]){ i++; } sb.append(romanArr[i]); n -= arabicArr[i]; } return sb.toString(); } }