Integer to Roman

本文介绍了一个将整数转换为罗马数字的算法,包括解析整数的千位、百位、十位和个位,并根据这些位上的数字构建对应的罗马字母。

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

没啥算法技术含量,搞明白Roman Integer的构造,按照千位,百位,十位和个位的数字来加一加就行了。

public class Solution {
    public String intToRoman(int num) {
        StringBuilder builder = new StringBuilder();
        int thousand = num / 1000;
        for(int i = 1; i <= thousand; i++){
            builder.append("M");
        }
        
        int hundred = num % 1000 / 100;
        //900
        if(hundred == 9)
            builder.append("CM");
        //0~400
        else if(hundred < 4){
            for(int i = 1; i <= hundred; i++)
                builder.append("C");
        }
        //400
        else if(hundred == 4){
            builder.append("CD");
        }
        //500~800
        else{
            builder.append("D");
            for(int i = 6; i <= hundred; i++)
                builder.append("C");
        }
        
        int tens = num % 100 / 10;
        //90
        if(tens == 9)
            builder.append("XC");
        //0~40
        else if(tens < 4){
            for(int i = 1; i <= tens; i++)
                builder.append("X");
        }
        //40
        else if(tens == 4){
            builder.append("XL");
        }
        //50~80
        else{
            builder.append("L");
            for(int i = 6; i <= tens; i++)
                builder.append("X");
        }
        
        int ones = num % 10;
         //9
        if(ones == 9)
            builder.append("IX");
        //0~4
        else if(ones < 4){
            for(int i = 1; i <= ones; i++)
                builder.append("I");
        }
        //4
        else if(ones == 4){
            builder.append("IV");
        }
        //5~8
        else{
            builder.append("V");
            for(int i = 6; i <= ones; i++)
                builder.append("I");
        }
        return builder.toString();
    }
}



在C语言中,`case label does not reduce to an integer constant` 错误通常意味着在 `switch` 语句里,`case` 标签使用了非整数常量表达式。`switch` 语句要求 `case` 标签必须是整数常量,像整型字面量、枚举常量或者宏定义的整数常量等。 ### 解决方案 #### 1. 确保 `case` 标签为整数常量 要保证 `case` 后面跟着的是整数常量。例如,下面的代码会产生此错误: ```c #include <stdio.h> int main() { int num = 2; switch (num) { case num: // 错误:num 不是整数常量 printf("This is a problem.\n"); break; default: printf("Default case.\n"); } return 0; } ``` 可将其修改为: ```c #include <stdio.h> int main() { int num = 2; switch (num) { case 2: // 正确:2 是整数常量 printf("This is okay.\n"); break; default: printf("Default case.\n"); } return 0; } ``` #### 2. 若处理字符串,不要用 `switch` 语句 `switch` 语句只能处理整数类型,若要处理字符串,可使用 `if-else if` 语句。例如,在罗马数字转数字的题目中,因为罗马数字是字符串,所以不能用 `switch` 语句: ```c #include <stdio.h> #include <string.h> int romanToInt(char * s) { int result = 0; if (strcmp(s, "I") == 0) { result = 1; } else if (strcmp(s, "V") == 0) { result = 5; } else if (strcmp(s, "X") == 0) { result = 10; } return result; } int main() { char roman[] = "V"; int num = romanToInt(roman); printf("The integer value is: %d\n", num); return 0; } ``` #### 3. 使用枚举类型 如果要处理多个有意义的整数值,可使用枚举类型。例如: ```c #include <stdio.h> enum RomanNumbers { I = 1, V = 5, X = 10 }; int main() { int num = V; switch (num) { case I: printf("Value is I (1).\n"); break; case V: printf("Value is V (5).\n"); break; case X: printf("Value is X (10).\n"); break; default: printf("Unknown value.\n"); } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值