整数转罗马数字
- 1 . 定义两数组对应关系,把题目的4,9,10,90,400,900也加上去
- 2 . 创建循环判断输入数据对应能用哪个符号
- 3 . while里面把当前符合能用的都拼接掉,跳出网小符号继续执行
class Solution {
public String intToRoman(int num) {
if(num<=0){
return null;
}
int[] number = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
String[] str = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
StringBuilder StringBuilder = new StringBuilder();
for(int i = str.length - 1;i>=0;i--){
while(number[i]<=num){
num = num - number[i];
StringBuilder.append(str[i]);
}
}
return StringBuilder.toString();
}
}