LeetCode


题目:

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

思路:

把几个特殊的值指明出来。

package manipulation;

public class IntegerToRoman {

public String intToRoman(int num) {
int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4 ,1};
String[] symbols
= {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
String res
= "";
for (int i = 0; num > 0 && i < 13; ++i) {
int times = num / nums[i];
num
= num % nums[i];
for (; times > 0; --times)
res
+= symbols[i];
}

return res;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
IntegerToRoman i = new IntegerToRoman();
System.out.println(i.intToRoman(
19));
}

}

 

本站声明
本文转载自:http://www.cnblogs.com/shuaiwhu/p/5048141.html     作者:Integer to Roman - NULL00     发布日期:2015/12/15     本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。


 
© 2014-2017 ITdaan.com 粤ICP备14056181号