码迷,mamicode.com
首页 > 编程语言 > 详细

Integer to Roman LeetCode Java

时间:2018-06-18 18:21:20      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:sys   pre   java   []   bsp   convert   eth   rom   span   

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

标签:sys   pre   java   []   bsp   convert   eth   rom   span   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!