12整数转罗马数字

这个题目原本很简单的一个Key value就搞定了
其实之前也有过类似的题目但是自己不行了

class Solution {

    public String intToRoman(int num) {

        String res = "";  // 初始化为空字符串

        String str = Integer.toString(num);  // 将数字转换为字符串形式

  

        // 处理千位数

        if (num / 1000 > 0) {

            while (num >= 1000) {

                res += "M";

                num -= 1000;

            }

            str = Integer.toString(num);  // 更新字符串表示

        }

  

        // 处理百位数

        if (num / 100 > 0) {

            if (str.charAt(0) == '4') {

                res += "CD";

                num -= 400;

            } else if (str.charAt(0) == '9') {

                res += "CM";

                num -= 900;

            } else {

                while (num >= 100) {

                    if (num >= 500) {

                        res += "D";

                        num -= 500;

                    }

                    while (num >= 100) {

                        res += "C";

                        num -= 100;

                    }

                }

            }

            str = Integer.toString(num);  // 更新字符串表示

        }

  

        // 处理十位数

        if (num / 10 > 0) {

            if (str.charAt(0) == '4') {

                res += "XL";

                num -= 40;

            } else if (str.charAt(0) == '9') {

                res += "XC";

                num -= 90;

            } else {

                while (num >= 10) {

                    if (num >= 50) {

                        res += "L";

                        num -= 50;

                    }

                    while (num >= 10) {

                        res += "X";

                        num -= 10;

                    }

                }

            }

            str = Integer.toString(num);  // 更新字符串表示

        }

  

        // 处理个位数

        if (num > 0) {

            if (num == 4) {

                res += "IV";

                num -= 4;

            } else if (num == 9) {

                res += "IX";

                num -= 9;

            } else {

                while (num >= 5) {

                    res += "V";

                    num -= 5;

                }

                while (num >= 1) {

                    res += "I";

                    num -= 1;

                }

            }

        }

  

        return res;  // 返回转换后的罗马数字

    }

}

第二种

class Solution {
    public String intToRoman(int num) {
        // 定义数值和对应的罗马数字符号
        int[] values = {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"};
        
        StringBuilder res = new StringBuilder();  // 使用StringBuilder来拼接字符串

        // 遍历每个数值和罗马符号
        for (int i = 0; i < values.length; i++) {
            // 当num大于或等于当前数值时,减去该数值并追加相应的罗马符号
            while (num >= values[i]) {
                num -= values[i];
                res.append(symbols[i]);
            }
        }

        return res.toString();  // 返回结果字符串
    }
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锦否

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值