当前位置:网站首页>13. Roman to Integer罗马数字转整数
13. Roman to Integer罗马数字转整数
2022-07-23 18:23:00 【DXB2021】
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I can be placed before V (5) and X (10) to make 4 and 9.
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X can be placed before L (50) and C (100) to make 40 and 90.
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C can be placed before D (500) and M (1000) to make 400 and 900.
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
Given a roman numeral, convert it to an integer.
给定一个罗马数字,将其转换成整数。
Example 1:
示例 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
输入: s = "III"
输出: 3
Example 2:
示例 4:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
输入: s = "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.
Example 3:
示例 5:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
输入: s = "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.
示例 2:
输入: s = "IV"
输出: 4
示例 3:
输入: s = "IX"
输出: 9
Constraints:提示:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M')
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内
题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。
IL 和 IM 这样的例子并不符合题目要求,49 应该写作 XLIX,999 应该写作 CMXCIX 。
关于罗马数字的详尽书写规则,可以参考 罗马数字 - Mathematics 。
C语言:失败
int romanToInt(char * s){
int num=0;
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='I')
{
if(s[i+1]=='V')
{
num+=4;
i++;
}
else if(s[i+1]='X')
{
num+=9;
i++;
}
else
num+=1;
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{
num+=40;
i++;
}
else if(s[i+1]=='C')
{
num+=90;
i++;
}
else
num+=10;
}
else if(s[i]=='C')
{
if(s[i+1]=='D')
{
num+=400;
i++;
}
else if(s[i+1]=='M')
{
num+=900;
i++;
}
else
num+=100;
}
else
{
if(s[i]=='V')
num+=5;
else if(s[i]=='L')
num+=50;
else if(s[i]=='D')
num+=500;
else if(s[i]=='M')
num+=1000;
}
}
return num;
}C语言:失败
int romanToInt(char * s){
int num=0;
for(int i=0;i<strlen(s);)
{
if(s[i]=='V')
{
num+=5;
i++;
}
else if(s[i]=='L')
{
num+=50;
i++;
}
else if(s[i]=='D')
{
num+=500;
i++;
}
else if(s[i]=='M')
{
num+=1000;
i++;
}
else if(s[i]=='I')
{
if(s[i+1]=='V')
{
num+=4;
i+=2;
}
else if(s[i+1]='X')
{
num+=9;
i+=2;
}
else
{
num+=1;
i++;
}
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{
num+=40;
i+=2;
}
else if(s[i+1]=='C')
{
num+=90;
i+=2;
}
else
{
num+=10;
i++;
}
}
else if(s[i]=='C')
{
if(s[i+1]=='D')
{
num+=400;
i+=2;
}
else if(s[i+1]=='M')
{
num+=900;
i+=2;
}
else
{
num+=100;
i++;
}
}
}
return num;
}C语言:
int romanToInt(char * s){
int num=0;
for(int i=0;i<strlen(s);i++)
{
switch(s[i])
{
case 'M':num+=1000;break;
case 'D':num+=500;break;
case 'L':num+=50;break;
case 'V':num+=5;break;
case 'C':switch(s[i+1])
{
case 'M':num-=100;break;
case 'D':num-=100;break;
default:num+=100;break;
}break;
case 'X':switch(s[i+1])
{
case 'L':num-=10;break;
case 'C':num=-10;break;
default:num+=10;break;
}break;
case 'I':switch(s[i+1])
{
case 'V':num-=1;break;
case 'X':num-=1;break;
default:num+=1;break;
}break;
}
}
return num;
}边栏推荐
- SecureCRT乱码问题解决方法[通俗易懂]
- [interview: concurrent Article 22 multithreading: reentrantlock]
- 时代潮头,华为将风帆对准数字金融的风与海
- Atcoder regular contest 144 [VP record]
- Usage of formatdatetime
- Basic process of process scheduling
- A preliminary study of the relationship between combinatorial mathematics and DP, and the derivation of resettable combinatorial formulas
- R语言ggpubr包的ggarrange函数多幅图像组合起来、annotate_figure组合图像添加注释、注解、标注信息、使用top参数在可视化图像顶部添加注解信息(自定义字体颜色、大小、样式)
- DHCP:在网络中防止 Rogue DHCP Server
- 【Unity项目实践】委托
猜你喜欢

时代潮头,华为将风帆对准数字金融的风与海

AtCoder Regular Contest 144【VP记录】

树莓派ssh登录

PowerCLi 导入 LicenseKey 到esxi

Type-C Bluetooth speaker single C-Port rechargeable OTG solution

DHCP:在网络中防止 Rogue DHCP Server

ACM mm 2022 oral | dig: the new framework of self-monitoring character recognition refreshes the recognition performance of 11 public scene character data sets, with an average improvement of 5%

我在代码里面故意留个漏洞,违法吗?

Codeforces Round #809 (Div. 2)【VP记录】

吃透Chisel语言.21.Chisel时序电路(一)——Chisel寄存器(Register)详解
随机推荐
Solutions to SecureCRT garbled code problem [easy to understand]
解密:智能化变电站中PTP时钟同步(北斗时钟服务器)
Publish the local image to Alibaba cloud warehouse
3D point cloud course (VI) -- 3D target detection
What are offline data and real-time data
三维点云课程(六)——三维目标检测
USB3.0:VL817Q7-C0的LAYOUT指南
MySQL 数据恢复 —— 使用 data 目录
我在代码里面故意留个漏洞,违法吗?
华为云HCS解决方案笔记HUAWEI CLOUD Stack【面试篇】
R language data The table package performs data grouping aggregation statistical transformations and calculates the grouping minimum value (min) of dataframe data
PowerCLi 管理VMware vCenter 批量部署导出导入
BoundsChecker用法「建议收藏」
Hongke dry goods | teaches you how to parse floating-point data in MODBUS
What is weak network testing? Why should weak network test be carried out? How to conduct weak network test? "Suggested collection"
R language mapping: coordinate axis setting
R语言data.table包进行数据分组聚合统计变换(Aggregating transforms)、计算dataframe数据的分组最小值(min)
paddle实现,多维时序数据增强 ,mixup(利用beta分布制作连续随机数)
入门数据库Days3
简历上写的电商,那请问Redis 如何实现库存扣减操作和防止被超卖?