Integer-to-roman

题目描述


Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.


code:
public class Solution {
    public String intToRoman(int num) {
        int[] number = {1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1};  
        String[] flags = {"M","CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};  
        StringBuilder sn = new StringBuilder();
        for(int i = 0 ; i < number.length ; i++)
        {
            while(num >= number[i])
            {
                num -= number[i];
                sn.append(flags[i]);
            }
        }
        return sn.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/neo233/article/details/80736616