欢迎来到天天文库
浏览记录
ID:59316260
大小:28.00 KB
页数:2页
时间:2020-09-05
《递归求n的阶乘.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#defineN6voidMatrixChain(intp[N+1],intn,intm[N+1][N+1],ints[N+1][N+1]){inti,j,r,k,t;for(i=1;i<=n;i++)m[i][i]=0;for(r=2;r<=n;r++)for(i=1;i<=n-r+1;i++){j=i+r-1;m[i][j]=m[i+1][j]+p[i-1]*p[i]*p[j];s[i][j]=i;for(k=i+1;k2、][j]){m[i][j]=t;s[i][j]=k;}}}}voidTraceback(inti,intj,ints[N+1][N+1])//输出最优解--矩阵的计算次序{if(i==j)return;Traceback(i,s[i][j],s);Traceback(s[i][j]+1,j,s);printf("multiplyA(%d,%d)&,A(%d,%d)",i,s[i][j],s[i][j]+1,j);printf("");}voidOutput(intn,inta[N+1][N+1]){inti,j;for(i=1;i<=n;i++){for(j3、=1;j<=n;j++)if((a[i][j]==0)&&(i!=j))printf("");elseprintf("%10d",a[i][j]);printf("");}}voidmain(){intp[N+1]={30,35,15,5,10,20,25},m[N+1][N+1]={0},s[N+1][N+1]={0};MatrixChain(p,N,m,s);Output(N,m);Output(N,s);Traceback(1,N,s);}
2、][j]){m[i][j]=t;s[i][j]=k;}}}}voidTraceback(inti,intj,ints[N+1][N+1])//输出最优解--矩阵的计算次序{if(i==j)return;Traceback(i,s[i][j],s);Traceback(s[i][j]+1,j,s);printf("multiplyA(%d,%d)&,A(%d,%d)",i,s[i][j],s[i][j]+1,j);printf("");}voidOutput(intn,inta[N+1][N+1]){inti,j;for(i=1;i<=n;i++){for(j
3、=1;j<=n;j++)if((a[i][j]==0)&&(i!=j))printf("");elseprintf("%10d",a[i][j]);printf("");}}voidmain(){intp[N+1]={30,35,15,5,10,20,25},m[N+1][N+1]={0},s[N+1][N+1]={0};MatrixChain(p,N,m,s);Output(N,m);Output(N,s);Traceback(1,N,s);}
此文档下载收益归作者所有