AnDrEi3008 Posted May 20, 2013 Report Posted May 20, 2013 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) Quote
Sega Posted May 20, 2013 Report Posted May 20, 2013 Posteaza un proof ca a fost facuta de tine. Altfel nu avem de unde sa stim ca nu cumva e tema ta de acasa. Quote
Silviu Posted May 20, 2013 Report Posted May 20, 2013 <?php function rome($N){ $c='IVXLCDM'; for($a=5,$b=$s='';$N;$b++,$a^=7) for($o=$N%$a,$N=$N/$a^0;$o--;$s=$c[$o>2?$b+$N-($N&=-2)+$o=1:$b].$s); return $s; } ?> Sursa: PHP: Hypertext Preprocessor Quote
AnDrEi3008 Posted May 20, 2013 Author Report Posted May 20, 2013 <? 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; } ?> Quote
Cryo Posted May 20, 2013 Report Posted May 20, 2013 Se mai tine challenge-ul?nop, silvian0 a stricat tot Quote
Silviu Posted May 20, 2013 Report Posted May 20, 2013 <? 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. Quote
AnDrEi3008 Posted May 20, 2013 Author Report Posted May 20, 2013 Daca reusesti sa il bati pe silvan0 poti sa incerci Quote
GarryOne Posted May 20, 2013 Report Posted May 20, 2013 <?php function rome($N){ $c='IVXLCDM'; for($a=5,$b=$s='';$N;$b++,$a^=7) for($o=$N%$a,$N=$N/$a^0;$o--;$s=$c[$o>2?$b+$N-($N&=-2)+$o=1:$b].$s); return $s; } ?> Sursa: PHP: Hypertext PreprocessorConditia era sa transformi din romane in arabe, tu ai facut invers. Quote
AnDrEi3008 Posted May 20, 2013 Author Report Posted May 20, 2013 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. atunci scuza-mi threadul. Quote
GarryOne Posted May 20, 2013 Report Posted May 20, 2013 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;} Quote
Sim Master Posted May 20, 2013 Report Posted May 20, 2013 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;} Quote
GarryOne Posted May 20, 2013 Report Posted May 20, 2013 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. Quote
Sim Master Posted May 21, 2013 Report Posted May 21, 2013 Am observat si eu cand am vrut sa-l postez. Quote