Jump to content
AnDrEi3008

[PHP] Competitie!

Recommended Posts

Posted

Conversie de la cifre romane la arabe!

Creati o functie care primeste ca parametru de intrare un sir de caractere reprezentand un numar scris cu cifre romane si care afiseaza numarul in cifre arabe.

A se folosi maxim 80 de caractere, cel care foloseste cat mai putine caractere castiga! ( experienta :)) ).

(Problema este data la un concurs vreau sa vad mai multe pareri)

Posted

<?

function f($a) {

if ($a < 0 || $a > 9999) { return -1; }

$r_p = array(1=> "I", 2=>"II", 3=>"III", 4=>"IV", 5=>"V", 6=>"VI", 7=>"VII", 8=>"VIII",

9=>"IX");

$r_z = array(1=> "X", 2=>"XX", 3=>"XXX", 4=>"XL", 5=>"L", 6=>"LX", 7=>"LXX",

8=>"LXXX", 9=>"XC");

$r_hund = array(1=> "C", 2=>"CC", 3=>"CCC", 4=>"CD", 5=>"D", 6=>"DC", 7=>"DCC",

8=>"DCCC", 9=>"CM");

$r_m = array(1=> "M", 2=>"MM", 3=>"MMM", 4=>"MMMM", 5=>"MMMMM", 6=>"MMMMMM",

7=>"MMMMMMM", 8=>"MMMMMMMM", 9=>"MMMMMMMMM");

$p = $a % 10;

$z = ($a - $p) % 100;

$s = ($a - $z - $p) % 1000;

$m = ($a - $s - $z - $p) % 10000;

$z = $z / 10;

$s = $s / 100;

$m = $m / 1000;

if ($m) { $r .= $r_m[$m]; }

if ($s) { $r .= $r_hund[$s]; }

if ($z) { $r .= $r_z[$z]; }

if ($p) { $r .= $r_p[$p]; }

return $r;

}

?>

Posted
<?

function f($a) {

if ($a < 0 || $a > 9999) { return -1; }

$r_p = array(1=> "I", 2=>"II", 3=>"III", 4=>"IV", 5=>"V", 6=>"VI", 7=>"VII", 8=>"VIII",

9=>"IX");

$r_z = array(1=> "X", 2=>"XX", 3=>"XXX", 4=>"XL", 5=>"L", 6=>"LX", 7=>"LXX",

8=>"LXXX", 9=>"XC");

$r_hund = array(1=> "C", 2=>"CC", 3=>"CCC", 4=>"CD", 5=>"D", 6=>"DC", 7=>"DCC",

8=>"DCCC", 9=>"CM");

$r_m = array(1=> "M", 2=>"MM", 3=>"MMM", 4=>"MMMM", 5=>"MMMMM", 6=>"MMMMMM",

7=>"MMMMMMM", 8=>"MMMMMMMM", 9=>"MMMMMMMMM");

$p = $a % 10;

$z = ($a - $p) % 100;

$s = ($a - $z - $p) % 1000;

$m = ($a - $s - $z - $p) % 10000;

$z = $z / 10;

$s = $s / 100;

$m = $m / 1000;

if ($m) { $r .= $r_m[$m]; }

if ($s) { $r .= $r_hund[$s]; }

if ($z) { $r .= $r_z[$z]; }

if ($p) { $r .= $r_p[$p]; }

return $r;

}

?>

Hai zi zau, ce seamana cu asta: https://www.mediawiki.org/wiki/Extension:RomanNumbers In afara de numele variabilelor totul e identic, chiar si asezarea.

Edit: Voi chiar puneti botul la asa ceva? Asta nu e challange e o prostie.

Posted

function RomanToArab($numar) {
$final = 0;
$romans = array("M" => 1000, "D" => 500, "C" => 100, "L" => 50, "X" => 10, "V" => 5, "I" => 1);
$romans_chars = "MDCLXVI";
for($i=0;$i<strlen($numar);$i++) {
$current_pos = strpos($romans_chars, $numar[$i]);
$next_char = @$romans_chars[$current_pos-1];
if(@$numar[$i+1] == $next_char)
$final = $final + $romans[$numar[$i+1]] - $romans[$numar[$i++]];
else
$final += $romans[$numar[$i]];
}
return $final;
}

Posted

function rtoa($num)
{
$num = strtoupper($num);
$numbers = array(
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000
);

$n = 0;
$p = 0;

while ($p < strlen($num))
{
if ($numbers[$num[$p]] < $numbers[$num[$p+1]])
{
$n += $numbers[$num[$p+1]]-$numbers[$num[$p]];
$p += 2;
}
else
{
$n += $numbers[$num[$p]];
$p++;
}
}

return $n;
}

Posted

function rtoa($num)
{
$num = strtoupper($num);
$numbers = array(
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000
);

$n = 0;
$p = 0;

while ($p < strlen($num))
{
if ($numbers[$num[$p]] < $numbers[$num[$p+1]])
{
$n += $numbers[$num[$p+1]]-$numbers[$num[$p]];
$p += 2;
}
else
{
$n += $numbers[$num[$p]];
$p++;
}
}

return $n;
}

Practic, e identic cu al meu, dar nu te acuz, stiu ca esti in stare si fara sa te uiti.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...