Leetcode12-整数转罗马数字

题目链接:12. 整数转罗马数字 - 力扣(LeetCode)

看题目限制输入1 <= num <= 3999,就直接用暴力法写了,还比较简单

代码:

char* intToRoman(int num) {
    char *res = (char*)malloc(100);
    int index = 0;
    int temp;
    temp = num / 1000;
    for (int i = 0; i < temp; i++) {
        res[index] = 'M';
        index++;
    }
    num = num % 1000;
    temp = num / 100;
    if (0 <= temp && temp <=3) {
        for (int i = 0; i < temp; i++) {
            res[index++] = 'C';
        }
    }
    if (temp == 4) {
        res[index++] = 'C';
        res[index++] = 'D';
    }
    if (5 <= temp && temp <= 8) {
        res[index++] = 'D';
        for (int i = 0; i < temp-5; i++) {
            res[index++] = 'C';
        }
    }
    if (temp == 9) {
        res[index++] = 'C';
        res[index++] = 'M';
    }
    num = num % 100;
    temp = num / 10;
    if (0 <= temp && temp <=3) {
        for (int i = 0; i < temp; i++) {
            res[index++] = 'X';
        }
    }
    if (temp == 4) {
        res[index++] = 'X';
        res[index++] = 'L';
    }
    if (5 <= temp && temp <= 8) {
        res[index++] = 'L';
        for (int i = 0; i < temp-5; i++) {
            res[index++] = 'X';
        }
    }
    if (temp == 9) {
        res[index++] = 'X';
        res[index++] = 'C';
    }
    temp = num % 10;
    if (0 <= temp && temp <=3) {
        for (int i = 0; i < temp; i++) {
            res[index++] = 'I';
        }
    }
    if (temp == 4) {
        res[index++] = 'I';
        res[index++] = 'V';
    }
    if (5 <= temp && temp <= 8) {
        res[index++] = 'V';
        for (int i = 0; i < temp-5; i++) {
            res[index++] = 'I';
        }
    }
    if (temp == 9) {
        res[index++] = 'I';
        res[index++] = 'X';
    }
    res[index] = '\0';

    return res;
}

LeetCode12 题中,要求将整数换为罗马数字。题目中规定输入的整数范围是 1 到 3999,而罗马数字的表示方法有特定规则,包括常规表示和一些特殊情况下的组合(如 IV、IX、XL 等)[^5]。 下面是一种清晰且高效的 C 语言实现方式,使用两个数组分别存储罗马数字对应的数值和符号,并通过循环匹配每次从输入整数中减去最大可能值的方式构建结果字符串[^2]: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> char* intToRoman(int num) { int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; char* symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; // 结果字符串的最大长度,根据实际情况调整 char* result = (char*)malloc(20 * sizeof(char)); result[0] = '\0'; // 初始化为空字符串 for (int i = 0; i < sizeof(values)/sizeof(values[0]); i++) { while (num >= values[i]) { strcat(result, symbols[i]); num -= values[i]; } } return result; } // 测试用例 int main() { int num = 1994; char* roman = intToRoman(num); printf("%s\n", roman); // 输出: MCMXCIV free(roman); return 0; } ``` ### 说明: - 使用了两个数组 `values` 和 `symbols` 来分别保存罗马数字对应的整数值及其符号。 - 通过遍历这两个数组,每次从输入整数中减去当前最大的罗马数值,并将其对应的符号拼接到结果字符串中。 -算法的时间复杂度为 O(1),因为罗马数字的种类是固定的,最多循环 13 次。 - 代码中动态分配了内存用于存储结果字符串,记得在使用完后调用 `free()` 释放内存以避免内存泄漏。 这种方法逻辑清晰,便于理解,并且能够很好地处理所有特殊组合情况[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

映秀小子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值