packagejuzhenliancheng;publicclassjuzhenliancheng{publicstaticvoidMatrixChain(int[]p,intn,int[][]m,int[][]s){for(inti=1;i<=n;i++){m[i][i]=0;}for(intr=2;r<=n;r++){for(inti...
package juzhenliancheng;
public class juzhenliancheng {
public static void MatrixChain(int []p,int n,int [][]m,int [][]s){
for(int i=1;i<=n;i++){
m[i][i]=0;
}
for(int r=2;r<=n;r++){
for(int i=1;i<=n-r+1;i++){
int j=i+r-1;
m[i][j]=m[i+1][j]+p[i-1]*p[i]*p[j];
s[i][j]=i;
for(int k=i+1;k
int t=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(t
m[i][j]=t;
s[i][j]=k;
}
}
}
}
}
public static void main(String[] args) {
// TODO 自动生成方法存根
int []p=new int[]{20,30,40,50};
int n=3;
int m[][]=new int[3][3];
int s[][]=new int[3][3];
MatrixChain(p, n, m, s);
for(int i=0;i<3;i++){
for(int k=0;k<3;k++){
System.out.print(m[i][k]);
}
System.out.println("");
}
}
}
展开