2018-08-12 12:52:35 +02:00
|
|
|
class Program {
|
2018-08-05 22:25:15 +02:00
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
private string num2Roman(int $num) {
|
2019-03-27 09:45:02 +01:00
|
|
|
int $n = $num;
|
2018-09-23 19:07:03 +02:00
|
|
|
string $result = '';
|
2019-03-21 17:33:40 +01:00
|
|
|
int[] $lookup = {'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
|
2018-08-05 22:25:15 +02:00
|
|
|
'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
|
2019-03-21 17:33:40 +01:00
|
|
|
'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1};
|
2018-08-05 22:25:15 +02:00
|
|
|
|
2018-09-23 17:51:47 +02:00
|
|
|
int $matches;
|
2019-05-01 11:51:08 +02:00
|
|
|
foreach(string $roman => int $value in $lookup) {
|
2019-03-23 17:33:29 +01:00
|
|
|
$matches = (int) ($n / $value);
|
2018-08-07 07:53:26 +02:00
|
|
|
$result += str_repeat($roman, $matches);
|
2018-08-05 22:25:15 +02:00
|
|
|
$n = $n % $value;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public void main() {
|
2018-08-07 07:53:26 +02:00
|
|
|
print(' 7 => ' + $this->num2Roman(7) + "\n");
|
|
|
|
print(' 9 => ' + $this->num2Roman(9) + "\n");
|
|
|
|
print(' 11 => ' + $this->num2Roman(11) + "\n");
|
|
|
|
print(' 42 => ' + $this->num2Roman(42) + "\n");
|
|
|
|
print(' 105 => ' + $this->num2Roman(105) + "\n");
|
|
|
|
print('1984 => ' + $this->num2Roman(1984) + "\n");
|
|
|
|
print('1999 => ' + $this->num2Roman(1999) + "\n");
|
|
|
|
print('2018 => ' + $this->num2Roman(2018) + "\n");
|
|
|
|
print('2144 => ' + $this->num2Roman(2144) + "\n");
|
2018-08-05 22:25:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|