Advertisement
juanjo12x

Lab2_Algoritmia_P4_Triangle_Sum

Apr 13th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. //problema 4 (piramide sumas)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. /*
  7.  *
  8.  */
  9.  
  10. int min(int,int );
  11. int main(int argc, char** argv) {
  12.     int i,j,b;
  13.     int M=100;
  14.     //printf("ingrese la cantidad de numeros en la base: ");
  15.     //scanf("%d",&b);
  16.     b=4;
  17.     int a[b][b]; // piramide
  18.     int sol[b][b];    // se puede construir la matriz con for tambien  
  19.     a[0][0]=2; a[0][1]=M ; a[0][2]=M ; a[0][3]=M;
  20.     a[1][0]=5; a[1][1]=4 ; a[1][2]=M ; a[1][3]=M;
  21.     a[2][0]=1; a[2][1]=4 ; a[2][2]=7 ; a[2][3]=M;
  22.     a[3][0]=8; a[3][1]=6 ; a[3][2]=9 ; a[3][3]=6;
  23.    
  24.     sol[0][0]=M; sol[0][1]=M ; sol[0][2]=M ; sol[0][3]=M;  // se puede construir la matriz con for tambien
  25.     sol[1][0]=M; sol[1][1]=M ; sol[1][2]=M ; sol[1][3]=M;
  26.     sol[2][0]=M; sol[2][1]=M ; sol[2][2]=M ; sol[2][3]=M;
  27.     sol[3][0]=M; sol[3][1]=M ; sol[3][2]=M ; sol[3][3]=M;
  28.     sol[0][0]=2;
  29.     for(i=1;i<b;i++){
  30.         for(j=0;j<=i;j++){// solucion tomando el elemento a[i][j]
  31.             if(j==0) sol[i][j]=sol[i-1][j]+a[i][j];
  32.             else
  33.                 sol[i][j]=min(sol[i-1][j-1],sol[i-1][j])+a[i][j];
  34.         }
  35.     }
  36.     int min=M;
  37.     for(i=0;i<b;i++){// hallo el minimo de la base con la suma obtenida en sol
  38.         if(sol[b-1][i]<=min)
  39.             min=sol[b-1][i];
  40.     }
  41.     printf("la suma minima de adyacentes es : %d \n",min);
  42.     return (EXIT_SUCCESS);
  43. }
  44. int min(int a , int b){
  45.     if(a<b) return a;
  46.     return b;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement