当前位置: 首页 > news >正文

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;
}

相关文章:

  • 使用ros_gz_bridge将gz topic转换成ros2 topic
  • 如何自动规整化(格式化)HTML
  • K8S学习之基础五十五:k8s中jenkins部署blueOcean
  • 每日免费分享之精品wordpress主题系列~DAY16
  • K8S安装及部署calico(亲测有用[特殊字符])
  • 基于SpringBoot + Vue 的餐厅点餐管理系统
  • 文件与fd
  • 海康设备http监听接收报警事件数据
  • Compare全目录文件比较内容(项目中用到过)
  • kafka零拷贝技术的底层实现
  • 【测试开发】OKR 小程序端黑盒测试报告
  • RabbitMQ三种队列深度解析:区别、场景与未来趋势
  • 【行驶证识别】批量咕嘎OCR识别行驶证照片复印件图片里的文字信息保存表格或改名字,基于QT和腾讯云api_ocr的实现方式
  • 速盾:Python可以用高防CDN吗?
  • 云服务器遭遇攻击怎么办
  • 阶段一:Java基础语法
  • 算法时间复杂度分析
  • 分布式特性对比
  • AI Agent:构建以数据为中心的智能体
  • Unity高清渲染管线
  • 阿森纳被打得毫无脾气,回天无力的阿尔特塔只剩嘴硬
  • 特色茶酒、非遗挂面……六安皋品入沪赴“五五购物节”
  • 中俄领导人将讨论从俄罗斯经蒙古至中国天然气管道项目?外交部回应
  • 住宿行业迎“最火五一”:数千家酒店连续3天满房,民宿预订量创历史新高
  • 市场监管总局发布《城镇房屋租赁合同(示范文本)》
  • 五一档观众最满意《水饺皇后》