罗马数字包含以下七种字符: I, V, X, LCD 和 M

字符          数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。

给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。

示例 1:

输入: 3
输出: "III"

示例 2:

输入: 4
输出: "IV"

示例 3:

输入: 9
输出: "IX"

示例 4:

输入: 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.

示例 5:

输入: 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
 char* intToRoman(int num) {
if (num < && num >) return NULL; char* ge[] = { "I","II","III","IV","V","VI","VII","VIII","IX" };
char* shi[] = { "X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
char* bai[] = { "C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
char* qian[] = { "M","MM","MMM" };
int figures = , remainder = ;
char * ge1=NULL, *shi1=NULL, *bai1=NULL, *qian1=NULL; char * roman_char = (char *)malloc(sizeof(char) * );
*roman_char = '\0';
/*
for ( int j = 0; j<15;j++){
roman_char[j] ='\0';
}
*/
// #define MAX 12
while (num != ) {
remainder = num % ;
num = num / ;
switch (figures)
{
case ://个位数
if (remainder == ) {
ge1 = NULL;
break;
}
ge1 = ge[remainder - ];
break;
case :
if (remainder == ) {
shi1 = NULL;
break;
}
shi1 = shi[remainder - ];
break;
case :
if (remainder == ) {
bai1 = NULL;
break;
}
bai1 = bai[remainder - ];
break;
case :
if (remainder == ) {
qian1 == NULL;
break;
}
qian1 = qian[remainder - ];
break;
default:
break;
}
figures++;
}
int lenth = ;
if (qian1 != NULL) {
lenth += strlen(qian1);
roman_char = realloc(roman_char, lenth); strcat(roman_char, qian1);
}
if (bai1 != NULL) {
lenth += strlen(bai1);
roman_char = realloc(roman_char, lenth);
strcat(roman_char, bai1); }
if (shi1 != NULL) {
lenth += strlen(shi1);
roman_char = realloc(roman_char, lenth);
strcat(roman_char, shi1);
}
if (ge1 != NULL) {
lenth += strlen(ge1);
roman_char = realloc(roman_char, lenth);
strcat(roman_char, ge1);
} return roman_char; }

LeetCode.数字转罗马数字的更多相关文章

  1. leetcode 12题 数字转罗马数字

    leetcode 12题 数字转罗马数字 答案一:我的代码 代码本地运行完全正确,在线运行出错 class Solution { public: string intToRoman(int num) ...

  2. LeetCode 13、罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 ...

  3. python刷LeetCode:13. 罗马数字转整数

    难度等级:简单 题目描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II  ...

  4. leetcode算法13.罗马数字转整数

    哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...

  5. 【LeetCode】13. 罗马数字转整数

    题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为 ...

  6. 力扣(LeetCode)13. 罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  7. 【LeetCode】将罗马数字转换成10进制数

    Roman to Integer Given a roman numeral, convert it to an integer. 首先介绍罗马数字 罗马数字共有七个,即I(1),V(5),X(10) ...

  8. 【LeetCode 13】罗马数字转整数

    题目链接 [题解] 就是上一题反过来的过程. 因为有说一般情况下后面的罗马数字是小于前面的罗马数字的. 如果前面的罗马数字小于后面的罗马数字了. 说明出现了4,9,40,90这些特殊情况. 那么就得判 ...

  9. [leetcode] 数字游戏

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

随机推荐

  1. Python百题计划

    一.基础篇 想要像类似执行shell脚本一样执行Python脚本,需要在py文件开头加上什么?KEY:#!/usr/bin/env python Python解释器在加载 .py 文件中的代码时,会对 ...

  2. filebeat-6.4.3-windows-x86_64输出Kafka

    配置filebeat.yml文件 filebeat.prospectors: - type: log encoding: utf- enabled: true paths: - e:\log.log ...

  3. Flutter控制屏幕旋转

    特定页面旋转屏幕很简单: SystemChrome.setPreferredOrientations([ ... ]); 数组中是您要支持的屏幕方向. 如果想在特定页面固定横屏, 您可以这样写: @o ...

  4. ubuntu beyond compare到期后续期

    rm -f /home/agu/.config/bcompare/registry.dat 或者加入定时任务,每天10:00执行 crontab -e * 10 * * * rm -f /home/a ...

  5. Manual write code to record error log in .net by Global.asax

    完整的Glabal.asax代码: <%@ Application Language="C#" %> <script RunAt="server&quo ...

  6. 获取SQL数据库中的数据库名、所有表名、所有字段名、列描述

    1.获取所有数据库名:    (1).Select Name FROM Master.dbo.SysDatabases orDER BY Name 2.获取所有表名:    (1).Select Na ...

  7. bzoj 1926: [Sdoi2010]粟粟的书架 (主席树+二分)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1926 题面; 1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Se ...

  8. docker添加阿里云专属镜像

    阿里云镜像地址:https://link.zhihu.com/?target=https%3A//cr.console.aliyun.com/%23/accelerator 根据提示开启容器镜像服务, ...

  9. OpenStack视图

    OpenStack视图 OpenStack视图是个全局资源的概念,统计了OpenStack所纳管资源的总量和使用量,因此OpenStack视图的资源通常又称为物理资源.OpenStack基于该资源使用 ...

  10. 指路Reactive Programming

    指路Reactive Programming Mar 02, 2016 in Engineering 我在工作中采用Reactive Programming(RP)已经有一年了,对于这个“新鲜”的辞藻 ...