码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]-011-Integer_to_Roman

时间:2016-04-18 11:38:31      阅读:132      评论: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 Solution{
 2     private String[][] arr = {
 3         {"0","I","II","III","IV","V","VI","VII","VIII","IX"},
 4         {"0","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
 5         {"0","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
 6         {"0","M","MM","MMM"}
 7     };
 8     public String intToRoman(int num) {
 9         if(num<0 || num>3999)
10             return "";
11         StringBuilder res = new StringBuilder();
12         int tmp = num;
13         if((tmp/1000)!=0)
14             res.append(arr[3][tmp/1000]);
15         tmp = tmp%1000;
16         if((tmp/100)!=0)
17             res.append(arr[2][tmp/100]);
18         tmp = tmp%100;
19         if((tmp/10)!=0)
20             res.append(arr[1][tmp/10]);
21         tmp = tmp%10;
22         if(tmp!=0)
23             res.append(arr[0][tmp]);
24         return res.toString();
25     }
26     
27     public static void main(String[] args){
28         int param = 1234;
29         if(args.length==1){
30             param = Integer.valueOf(args[0]);
31         }
32         Solution solution = new Solution();
33         String res = solution.intToRoman(param);
34         System.out.println(res);
35     }
36 }

 

[LeetCode]-011-Integer_to_Roman

标签:

原文地址:http://www.cnblogs.com/lianliang/p/5403615.html

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