首页 > 编程语言 > 详细

Integer to Roman LeetCode Java

时间:2018-06-18 18:02:37      阅读:166      评论:0      收藏:0      [点我收藏+]

描述
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 }

 

Integer to Roman LeetCode Java

原文:https://www.cnblogs.com/ncznx/p/9195594.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!