Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
以上是题目要求
想做这道题必须要明白罗马数字是怎么写的
阿拉伯数字 | 罗马数字 |
1 | I |
4 | IV |
5 | V |
9 | IX |
10 | X |
40 | XL |
50 | L |
90 | XC |
100 | C |
400 | CD |
500 | D |
900 | CM |
1000 | M |
package Q12IntegerToRoman;
import java.util.Scanner;
/**
* @author Bryan
* @since 2017/11/2 18:45
*/
public class Solution {
public static String intToRoman(int num) {
String str = "";
String[] strings = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int[] values = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
for (int i = 12 ; num !=0 ; i --) {
while (num >= values[i]) {
num -= values[i];
str += strings[i];
}
}
return str;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
System.out.println(intToRoman(num));
}
}