本文主要是介绍ACM从零开始---电子科大POJ 任意阶矩阵相乘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
任意阶矩阵的乘法
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
C-source:
<span style="color:#333333;">#include<stdio.h>
#include<malloc.h>
int main(void)
{int m,g,n;int i,j,k;int number;double **a=NULL,**b=NULL,**result=NULL;printf("Please input number:\n");scanf("%d",&number);while(number!=0)
{number--;printf("Please input m,g,n:\n");scanf("%d%d%d",&m,&g,&n);a=(double**)malloc(m*sizeof(double*));for(i=0;i<m;i++)a[i]=(double*)malloc(g*sizeof(double));printf("Please input the first jz:\n");for(i=0;i<m;i++)for(j=0;j<g;j++)scanf("%lf",&a[i][j]);b=(double**)malloc(g*sizeof(double *));for(i=0;i<g;i++)b[i]=(double*)malloc(n*sizeof(double));printf("Please input the second jz\n");for(i=0;i<g;i++)for(j=0;j<n;j++)scanf("%lf",&b[i][j]);result=(double**)malloc(m*sizeof(double*));for(i=0;i<m;i++)result[i]=(double*)malloc(n*sizeof(double));for(i=0;i<m;i++)for(j=0;j<n;j++)result[i][j]=0;for(i=0;i<m;i++)for(j=0;j<n;j++)for(k=0;k<g;k++)result[i][j]+=a[i][k]*b[k][j];printf("answer:\n");for(i=0;i<m;i++){for(j=0;j<n;j++)printf("%5g",result[i][j]);printf("\n");}
}for(i=0;i<m;i++){free(a[i]);a[i]=NULL;free(result[i]);result[i]=NULL;}free(a);a=NULL;free(result);result=NULL;for(i=0;i<g;i++){ free(b[i]);b[i]=NULL;}free(b);b=NULL;return 0;
}</span><span style="color:#ff0000;">
</span>
这篇关于ACM从零开始---电子科大POJ 任意阶矩阵相乘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!