[LeetCode]-011-Integer_to_Roman

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 }

 

posted @ 2016-04-18 10:56  练子  阅读(150)  评论(0编辑  收藏  举报