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