Integer to Roman LeetCode Java

描述
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析

代码

 1 public class IntegerToRoman {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         int num = 187;
 6         System.out.println(intToRoman(num));
 7     }
 8 
 9     public static String intToRoman(int num) {
10         int radix[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
11         String symbol[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
12         String roman = "";
13         for (int i = 0; num > 0; ++i) {
14             int count = num / radix[i];
15             num %= radix[i];
16             for (; count > 0; --count)
17                 roman += symbol[i];
18         }
19         return roman;
20     }
21 
22 }

猜你喜欢

转载自www.cnblogs.com/ncznx/p/9195594.html