整数转罗马数字

题目描述:给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。I对应1,V对应5,X对应10,L对应50,C对应100,D对应500,M对应1000

示例:输入: 1994 输出: "MCMXCIV" 解释: M = 1000, CM = 900, XC = 90, IV = 4.

java代码

class Solution {

    private static final int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};

    private static final String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

    public String intToRoman(int num) {

        StringBuilder res = new StringBuilder();

        int k = 0;

        while (num>0) {

            int count = num / values[k];

            for (int i = 0;i

                res.append(symbols[k]);

                num -= values[k];

            }

            k++;

        }

        return res.toString();

    }

}

你可能感兴趣的:(整数转罗马数字)