参考:https://blog.csdn.net/qq_32919451/article/details/80643118
http://www.cnblogs.com/PJQOOO/p/4474354.html
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N=100; 4 const int inf=0x37373737; 5 int in[N],s[N][N],dp[N][N];//s[]放间断点 6 int n; 7 void test() 8 { 9 for (int i=1;i<=n;i++) 10 { 11 for (int j=1;j<=n;j++) 12 { 13 printf("[%9d]",dp[i][j]); 14 } 15 printf(" "); 16 } 17 printf(" "); 18 } 19 void dpp() 20 { 21 for (int len=1;len<=n;len++) 22 { 23 for (int i=1;i<=n-len+1;i++)//注意i的边界!!! 24 { 25 int j=i+len-1; 26 if (i==j) 27 { 28 dp[i][j]=0; 29 continue; 30 } 31 dp[i][j]=inf; 32 // printf("%d %d ",i,j); 33 // test(); 34 for (int k=i+1;k<=j;k++)//注意k和j的关系!!! 35 { 36 // printf("k%d ",k); 37 int temp=dp[i][k-1]+dp[k][j]+in[i]*in[k]*in[j+1]; 38 // printf("%d=%d,%d+%d,%d+%d*%d*%d ",temp,i,k-1,k,j,i,k,j+1); 39 if (temp<dp[i][j]) 40 { 41 dp[i][j]=temp; 42 s[i][j]=k; 43 } 44 } 45 } 46 } 47 } 48 int main() 49 { 50 freopen("in.txt","r",stdin); 51 printf("input: "); 52 scanf("%d",&n); 53 for (int i=1;i<=n;i++) 54 { 55 int ta,tb; 56 scanf("%d%d",&ta,&tb); 57 if (i==1) 58 { 59 in[1]=ta; 60 } 61 in[i+1]=tb; 62 } 63 for (int i=1;i<=n;i++) 64 { 65 printf("%d*%d ",in[i],in[i+1]); 66 } 67 printf(" "); 68 dpp(); 69 test(); 70 71 return 0; 72 }