将罗马数字转换为1到3999之间十进制数的PHP程序
基于前罗马罗马体系的数字表示法排列中使用的字符称为罗马数字。以下部分涵盖了所有主要符号。在本问题中,我们给定一个罗马数字字符串,我们的任务是将罗马数字转换为1到3999范围内的十进制数。
以下是一些示例和说明,以帮助您更好地理解问题。
输入
str = "DCCCLXXIV"
输出
str = 874
解释
DCCC是800的罗马表示法,其中D表示500,C表示100。
LXX是70的罗马表示法,其中L表示50,X表示10,
而IV是4的罗马表示法。
输入
"CMXCIX"
输出
999
解释
CM是900的罗马表示法,M表示1000,C表示100(比1000少100),
类似地,XC是90的罗马表示法,C表示100,X表示10(比100少10),
同样,IX是9的罗马表示法。
输入
"I"
输出
1
解释
I是1的罗马表示法。
在进入方法之前,让我们仔细看看主要的罗马符号。罗马数字完全构建在以下符号的基础上。
| 符号 | 值 |
|---|---|
| M | 1000 |
| CM | 900 |
| D | 500 |
| CD | 400 |
| C | 100 |
| XC | 90 |
| L | 50 |
| XL | 40 |
| X | 10 |
| IX | 9 |
| V | 5 |
| IV | 4 |
| I | 1 |
方法
我们已经看到了上面给定罗马数字字符串的示例,让我们来看一下方法。
根据观察,罗马数字符号遵循降序来表示数字(例如,M先出现,然后是C等)。但是,它在某些情况下也遵循减法表示法,以防止连续重复4个字符(例如XXXX或CCCC)。
C在D和M之前表示少100,例如:
-> 400用罗马数字表示为CD(比五百少一百)
-> 900用罗马数字表示为CM(比一千少一百)
X在L或C之前表示少十,例如:
-> 40用罗马数字表示为XL(比五十少十),
-> 90用罗马数字表示为XC(比一百少十)
I在V或X之前表示少一,例如:
-> 4用罗马数字表示为IV(比五少一),
-> 9用罗马数字表示为IX(比十少一)
让我们看看下面的代码,以便更好地理解上述方法。
示例
将罗马数字转换为十进制数字的PHP程序 创建函数“romanValue”以返回罗马符号的值
<?php
function romanValue($ch){
// intializing the value to store decimal value of roman symbol
$val = -1;
if ($ch == 'I')
$val = 1;
else if ($ch == 'V')
$val = 5;
else if ($ch == 'X')
$val = 10;
else if ($ch == 'L')
$val = 50;
else if ($ch == 'C')
$val = 100;
else if ($ch == 'D')
$val = 500;
else if ($ch == 'M')
$val = 1000;
return $val;
}
// created a function to return decimal value of given roman value
function convertRomanToDecimal(&$str){
// create variable decValue that we have to return and assign 0 to it
$decValue = 0;
$n = strlen($str); // Getting the size of the given string
// calculate decValue while traversing the given string using for loop
for ($i = 0; $i < $n; $i++) {
// Store decimal value of romanValue str[i]
$current = romanValue($str[$i]);
// check i+1 char exist
if ($i+1 < $n) {
// Store the decimal value of romanValue str[i+1]
$next = romanValue($str[$i + 1]);
// check which value is greater current or next
if ($current >= $next) {
// if current value >= next value add value to the decValue
$decValue = $decValue + $current;
} else {
// if current value < next value then add difference of value of next to current to the decValue
$decValue = $decValue + $next - $current;
// Increment the index of the string to point to the next char
$i++;
}
}
// If i+1 char not exist
else {
// Add current value to the decValue variable
$decValue = $decValue + $current;
// Increment the index of the string to point to the next char
$i++;
}
}
// Return decimal value
return $decValue;
}
$str ="DCCCLXXIV"; // Given Roman numeral string
// Print the decimal form and call the function of conversion
echo "The decimal Numeral form of the Roman Numeral is " . convertRomanToDecimal($str) . "";
?>
输出
The decimal Numeral form of the Roman Numeral is 874
时间和空间复杂度
上面代码的时间复杂度为O(N),因为只需要遍历一次字符串。其中N是给定罗马数字字符串的大小。并且由于没有使用额外的空间来存储任何东西,所以上面代码的空间复杂度为O(1)。
结论
在本教程中,我们实现了将罗马数字转换为1到3999之间十进制数的PHP程序。我们实现了一种方法,其中创建一个函数来获取罗马值的对应十进制值。这种方法的时间复杂度为O(N),其中N是字符串的大小,空间复杂度为O(1),因为没有使用额外的空间。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP