整数转罗马数字 (Integer to Roman)

 

思路:

// @Title: 整数转罗马数字 (Integer to Roman)
// @Author: qisiii
// @Date: 2024-04-11 23:31:56
// @Runtime: 3 ms
// @Memory: 43.1 MB
// @comment: 
// @flag: 
class Solution {
    public String intToRoman(int num) {
        int[] ala=new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String[] lm=new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        StringBuilder str=new StringBuilder();
        for(int i=0;i<ala.length;i++){
            //2999
            while(num>=ala[i]){
                str.append(lm[i]);
                num=num-ala[i];
            }
        }
        return str.toString();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18