电子科大POJ "任意阶矩阵相乘"

       任意阶矩阵的乘法


  Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)



C-source:
 1 <span style="color:#333333;">#include<stdio.h>
 2 #include<malloc.h>
 3 int main(void)
 4 {
 5  int m,g,n;
 6  int i,j,k;
 7 
 8  int number;
 9  double **a=NULL,**b=NULL,**result=NULL;
10  printf("Please input number:\n");
11  scanf("%d",&number);
12 
13 while(number!=0)
14 {
15     number--;
16 
17     printf("Please input m,g,n:\n");
18     scanf("%d%d%d",&m,&g,&n);
19 
20 
21     a=(double**)malloc(m*sizeof(double*));
22     for(i=0;i<m;i++)
23         a[i]=(double*)malloc(g*sizeof(double));
24 
25     printf("Please input the first jz:\n");
26     for(i=0;i<m;i++)
27         for(j=0;j<g;j++)
28         scanf("%lf",&a[i][j]);
29 
30     b=(double**)malloc(g*sizeof(double *));
31     for(i=0;i<g;i++)
32         b[i]=(double*)malloc(n*sizeof(double));
33 
34     printf("Please input the second jz\n");
35     for(i=0;i<g;i++)
36         for(j=0;j<n;j++)
37             scanf("%lf",&b[i][j]);
38 
39     result=(double**)malloc(m*sizeof(double*));
40         for(i=0;i<m;i++)
41             result[i]=(double*)malloc(n*sizeof(double));
42 
43     for(i=0;i<m;i++)
44         for(j=0;j<n;j++)
45             result[i][j]=0;
46 
47     for(i=0;i<m;i++)
48         for(j=0;j<n;j++)
49             for(k=0;k<g;k++)
50                 result[i][j]+=a[i][k]*b[k][j];
51 
52     printf("answer:\n");
53     for(i=0;i<m;i++)
54     {
55         for(j=0;j<n;j++)
56             printf("%5g",result[i][j]);
57             printf("\n");
58     }
59 }
60     for(i=0;i<m;i++)
61     {
62         free(a[i]);
63         a[i]=NULL;
64         free(result[i]);
65         result[i]=NULL;
66     }
67     free(a);
68     a=NULL;
69     free(result);
70     result=NULL;
71 
72 
73     for(i=0;i<g;i++)
74     {   free(b[i]);
75         b[i]=NULL;
76     }
77     free(b);
78     b=NULL;
79 
80     return 0;
81 }

 


 

 

posted @ 2014-07-03 09:36  vpoet  阅读(177)  评论(0编辑  收藏  举报