Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/03/2012, 09:26
SideEfect
 
Fecha de Ingreso: mayo-2011
Mensajes: 38
Antigüedad: 13 años
Puntos: 0
Pregunta triangulo con suma maxima

buenos dias a todos ,he estado teniendo problemas con el siguiente programa ya que es un triangulo que sigue una secuencia de suma en forma de escalera ,sin embargo no logro hallar la suma correcta no se q estare haciendo mal .....
este es el ejercicio real por si no me entendieron

TRIANGLE PUZZLE

By starting at the top of the triangle and moving to adjacent numbers on the row below, the maximum total from top to bottom is 27..

5
9 6
4 6 8
0 7 1 5

I.e. 5 + 9 + 6 + 7 = 27.

Write a program in PHP to find the maximum total from top to bottom in the text file , a text file containing a triangle with 100 rows. What is the maximum sum for the triangle?????
Código PHP:
Ver original
  1. <?php
  2.             error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3.          $i=0;
  4.          $j=0;
  5.          $x=0;         
  6.     $sum=0;
  7.         $sum2=0;
  8.     if($_POST['calcular']){
  9.         echo ("<br>");
  10.            
  11.     for($i=1;$i<=4;$i++){
  12.          
  13.            
  14.         for($j=1;$j<=$i;$j++){
  15.    
  16.        
  17.             $x= mt_rand(0,100);
  18.             $vector[$j]=$x;
  19.        
  20.        
  21.            
  22.     $z=$vector[$j];
  23.                  echo ("   ". $z);}
  24.                 echo ("<br>");
  25.            
  26.            
  27.     }
  28.    
  29.       $c = 1;
  30.     for($a=1;$a<=4;$a++){
  31.     if($a > 1){
  32.         if($vector[$c-1] > $vector[$c]){
  33.                 $sum = $sum + $vector[$c-1];
  34.                
  35.                 }
  36.                 else
  37.                 {
  38.                     $sum = $sum + $vector[$c];
  39.                
  40.                 $c++;
  41.                
  42.                 }
  43.                
  44.                 }
  45.                
  46.                 }
  47.      echo ("la suma es  ".$sum);   
  48.                
  49.     }
  50.  
  51.            ?>