將羅馬數字轉換為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