标签:java leetcode 解题代码 java leetcode
题目:Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
翻译:
给一个整数,把他转换为罗马数字输出。
这道题是简单的字符转换的问题。只要了解罗马数字代表的含义即可。
罗马数字里面只有 1,5,10. 1到3之间用重复的1表示,4用IV 90用XC 依次类推。
I = 1;
V = 5;
X = 10;
L = 50;
C = 100;
D = 500;
M = 1000;
代码:
public static String intToRoman(int num) { String str =""; String roman[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int number[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; for(int i = 0 ;num!=0 ;i++) { while(num >= number[i]) { num-=number[i]; str+=roman[i]; } } return str; }
Leetcode 12 Integer to Roman整数变罗马
标签:java leetcode 解题代码 java leetcode
原文地址:http://blog.csdn.net/vvaaiinn/article/details/45193631