fork download
  1.  
  2.  
  3. /* I HAVE ALLOCATED MEMORY STATICALLY FOR TESTING PURPOSES.
  4.  
  5. P[ ]={5,4,6,2,7}
  6.  
  7. means:
  8.  
  9. matrices of order 5 X 4 , 4 X 6 , 6 X 2 , 2 X 7
  10.  
  11. check http://w...content-available-to-author-only...t.hk/~dekai/271/notes/L12/L12.pdf
  12.  
  13. for the dry run and the detailed algorithm.
  14.  
  15.  
  16. */
  17.  
  18.  
  19. #include<stdio.h>
  20.  
  21. int main()
  22. {
  23. int p[]={5,4,6,2,7};
  24. int a[5][5];
  25.  
  26.  
  27.  
  28. int i,j,k,gap=1;
  29.  
  30.  
  31. for(i=0;i<5;i++)
  32. for(j=0;j<5;j++)
  33. a[i][j]=30000;
  34.  
  35. for(gap=0;gap<5;gap++)
  36. {
  37. for(i=1,j=i+gap ; i<5 && j<5 ; i++,j++)
  38. {
  39. if(i==j) a[i][i]=0;
  40. else
  41. {
  42.  
  43. for(k=i;k<j;k++)
  44. {
  45. int val=a[i][k]+a[k+1][j] + p[i-1]*p[k]*p[j];
  46. if(val<a[i][j])
  47. {
  48. a[i][j]=val;
  49.  
  50. }
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. }
  58.  
  59. for(i=1;i<5;i++)
  60. { for(j=1;j<5;j++)
  61. {
  62. if(a[i][j]==30000) printf("\tX");
  63. else printf("\t %d",a[i][j]);
  64.  
  65. }
  66. printf("\n") ;
  67. }return 0;
  68. }
  69.  
Success #stdin #stdout 0s 2724KB
stdin
Standard input is empty
stdout
	 0	 120	 88	 158
	X	 0	 48	 104
	X	X	 0	 84
	X	X	X	 0