zoukankan      html  css  js  c++  java
  • 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                 
    keep moving...
  • 相关阅读:
    将博客搬至CSDN
    mplayer的执行过程分析(本地、网络)
    ES6新特性概览
    ES6 块级作用域
    echarts各种字体颜色的修改
    js时间格式转换为几天前几小时几分钟等
    正则验证非法字符
    JS阻止事件冒泡函数
    cookie设置,读取,删除
    C/C++基础(三)
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3045511.html
Copyright © 2011-2022 走看看