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

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


http://www.kler.cn/a/611572.html

相关文章:

  • 数据库基础知识点(系列七)
  • 【开题报告+论文+源码】基于SpringBoot的智能安全与急救知识科普系统设计与实现
  • 【后端】【Django DRF】从零实现RBAC 权限管理系统
  • Ubuntu22.04搭建freeradius操作说明
  • Java基础 3.22
  • 深度学习入门1 基于Python的理论与实现
  • 如何在 Postman 中发送 DELETE 请求?
  • ngx_conf_handler - root html
  • GPT Researcher 的win docker安装攻略
  • unity泛型对象池
  • Elasticsearch:使用 AI SDK 和 Elastic 构建 AI 代理
  • JVM
  • Occlum 是一个内存安全的、支持多进程的 library OS,特别适用于 Intel SGX。
  • 基于VMware虚拟机的Ubuntu22.04系统安装和配置(新手保姆级教程)
  • b站视频字幕提取拓展推荐
  • 腾讯四面面经
  • 架构思路法
  • 批量给 PDF 页面添加超链接
  • Android 自定义变形 SHA1 算法
  • 【含文档+PPT+源码】基于SpringBoot+Vue的贫困地区留守儿童关怀系统