leetcode12.整数转罗马数字
循环数值由大到小以此拼接就行
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
class Solution {
// 定义罗马数字与对应数值的数组
private static final Entry<Integer, String>[] valueSymbols = new SimpleEntry[]{
new SimpleEntry<>(1000, "M"),
new SimpleEntry<>(900, "CM"),
new SimpleEntry<>(500, "D"),
new SimpleEntry<>(400, "CD"),
new SimpleEntry<>(100, "C"),
new SimpleEntry<>(90, "XC"),
new SimpleEntry<>(50, "L"),
new SimpleEntry<>(40, "XL"),
new SimpleEntry<>(10, "X"),
new SimpleEntry<>(9, "IX"),
new SimpleEntry<>(5, "V"),
new SimpleEntry<>(4, "IV"),
new SimpleEntry<>(1, "I")
};
public String intToRoman(int num) {
StringBuilder roman = new StringBuilder();
for (Entry<Integer, String> entry : valueSymbols) {
int value = entry.getKey();
String symbol = entry.getValue();
while (num >= value) {
num -= value;
roman.append(symbol);
}
if (num == 0) {
break;
}
}
return roman.toString();
}
}