POJ1993+矩阵相乘

给n个矩阵,求出相乘后的结果

View Code
 1 #include<stdio.h>
 2 const int maxn = 105;
 3 struct node{
 4     int row,col;
 5     int mat[ maxn ][ maxn ];
 6 };
 7 node res;
 8 int main(){
 9     int ca;
10     scanf("%d",&ca);
11     while( ca-- ){
12         int x;
13         scanf("%d",&x);
14         node a,b;
15         int n,m;
16         scanf("%d%d",&n,&m);
17         for( int i=0;i<n;i++ ){
18             for( int j=0;j<m;j++ ){
19                 scanf("%d",&a.mat[i][j]);
20             }
21         }
22         a.row = n;
23         a.col = m;
24         res = a;
25         
26         //printf("row:%d col:%d\n",res.row,res.col);
27         
28         x--;
29         while( x-- ){
30             int n,m;
31             scanf("%d%d",&n,&m);
32             for( int i=0;i<n;i++ ){
33                 for( int j=0;j<m;j++ ){
34                     scanf("%d",&a.mat[i][j]);
35                 }
36             }
37             a.row = n;
38             a.col = m;
39             for( int i=0;i<res.row;i++ ){
40                 for( int j=0;j<a.col;j++ ){
41                     b.mat[i][j] = 0;
42                     for( int k=0;k<res.col;k++ ){
43                         b.mat[i][j]+=( res.mat[i][k]*a.mat[k][j] );
44                     }
45                 }
46             }
47             b.row = res.row;
48             b.col = a.col;
49             res = b;
50             
51             //printf("row:%d col:%d\n",res.row,res.col);
52             
53         }
54         for( int i=0;i<res.row;i++ ){
55             for( int j=0;j<res.col;j++ ){
56                 if( j==0 ) printf("%d",res.mat[i][j]);
57                 else printf(" %d",res.mat[i][j]);
58             }
59             printf("\n");
60         }
61         printf("\n");
62     }
63     return 0;
64 }
65                 

 

posted @ 2013-04-26 19:05  xxx0624  阅读(223)  评论(0编辑  收藏  举报