-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.php
More file actions
71 lines (58 loc) · 1.61 KB
/
IntegerToRoman.php
File metadata and controls
71 lines (58 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php https://leetcode.com/problems/integer-to-roman/
/**
* @param Integer $num
* @return String
*/
function intToRoman($num) {
$unidades = [0=>'',1=>'I',2=>'II',3=>'III',4=>'IV',5=>'V',6=>'VI',7=>'VII',8=>'VIII',9=>'IX'];
$decenas = [0=>'',1=>'X',2=>'XX',3=>'XXX',4=>'XL',5=>'L',6=>'LX',7=>'LXX',8=>'LXXX',9=>'XC'];
$centenas = [0=>'',1=>'C',2=>'CC',3=>'CCC',4=>'CD',5=>'D',6=>'DC',7=>'DCC',8=>'DCCC',9=>'CM'];
$millares = [0=>'',1=>'M',2=>'MM',3=>'MMM'];
$strnum = (string) $num;
$longitud = strlen($strnum);
if ($longitud == 1) {
$roman = $unidades[ $strnum[0] ];
return $roman;
}
if ($longitud == 2) {
$roman = '';
$roman .= $decenas[ $strnum[0] ];
$roman .= $unidades[ $strnum[1] ];
return $roman;
}
if ($longitud == 3) {
$roman = '';
$roman .= $centenas[ $strnum[0] ];
$roman .= $decenas[ $strnum[1] ];
$roman .= $unidades[ $strnum[2] ];
return $roman;
}
if ($longitud == 4) {
$roman = '';
$roman .= $millares[ $strnum[0] ];
$roman .= $centenas[ $strnum[1] ];
$roman .= $decenas[ $strnum[2] ];
$roman .= $unidades[ $strnum[3] ];
return $roman;
}
}
if ( intToRoman(3) == "III" ){
echo "correcto<br>";
}else{
echo "incorrecto<br>";
}
if ( intToRoman(58) == "LVIII" ){
echo "correcto<br>";
}else{
echo "incorrecto<br>";
}
if ( intToRoman(102) == "CII" ){
echo "correcto<br>";
}else{
echo "incorrecto<br>";
}
if ( intToRoman(1994) == "MCMXCIV" ){
echo "correcto<br>";
}else{
echo "incorrecto<br>";
}