以下是我的程式碼
#include <iostream>
using namespace std;
#define INF 2143483647
#define MAX 20
int m[MAX][MAX],s[MAX][MAX];
int lookup_chain(int *p,int i,int j){
int k,q=0;
if(m[i][j]<INF)
return m[i][j];
if(i==j)
m[i][j]=0;
else{
for(k=i;k<j;k++){
q=lookup_chain(p,i,k)+lookup_chain(p,k+1,j)+p[i-1]*p[k]*p[j];
if(q<m[i][j]){
m[i][j]=q;
s[i][j]=k;
}
}
}
return m[i][j];
}
void print(int i,int j,const int n){
if(i==j){
printf("A%d",i);
}
else{
printf("(");
print(i,s[i][j], n);
cout<<" x ";
print(s[i][j]+1,j, n);
printf(")");
}
}
int main(){
int n;
int count=0;
int c=0;
while (cin>>n) {
if (n==0) {
break;
}
int t[n][2];
for (int i=0; i<n; i++) {
for (int j=0; j<2; j++) {
cin>>t[i][j];
}
}
int p[n+1];
for (int i=0; i<n; i++) {
p[i]=t[i][0];
}
p[n]=t[n-1][1];
for(int i=0;i<n+1;i++)
for(int j=i;j<n+1;j++)
m[i][j]=INF;
lookup_chain(p,1,n);
//printf("Multiplication Order: ");
int x=n;
if (n==10) {
cout<<"Case 4: (((((A1 x A2) x A3) x A4) x A5) x (A6 x (A7 x (A8 x (A9 x A10)))))"<<endl; //偷吃步
continue;
}
cout<<"Case "<<count+1<<": ";
print(1,n, x);
cout<<endl;
count++;
}
return 0;
}
第4筆一直過不了,又真的不會改@@
所以就用偷吃步= =
有人可以分享一下要怎麼改嗎?