NYOJ-623:A*B Problem II

来源:互联网 发布:黑马西安java培训机构 编辑:程序博客网 时间:2024/04/29 23:21
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define N 100class Matrix{public:void Init(){memset(data,0,sizeof(data));}void Matrix_multiply(Matrix & A,Matrix & B){if (A.col!=B.row){printf("A.col!=B.row\n");return;}Init();row = A.row, col = B.col;int i,j,k;for (i=0;i<A.row;i++)for(j=0;j<B.col;j++){data[i][j] = 0;for(k=0;k<A.col;k++)data[i][j] += A.data[i][k] * B.data[k][j];}}void Matrix_add(Matrix & A,Matrix & B){if (A.row!=B.row || A.col!=B.col){printf("Error ! \n");return;}Init();row = A.row, col = A.col;int i,j;for(i=0;i<A.row;i++)for(j=0;j<A.col;j++)data[i][j] = A.data[i][j] + B.data[i][j];}void Matrix_sub(Matrix & A,Matrix & B){Init();if (A.row!=B.row || A.col!=B.col){printf("Error ! \n");return;}row = A.row, col = A.col;int i,j;for(i=0;i<A.row;i++)for(j=0;j<A.col;j++)data[i][j] = A.data[i][j] - B.data[i][j];}void Output(){int i,j;for(i=0;i<row;i++){for(j=0;j<col;j++)cout<<(j==0?"":" ")<<data[i][j];cout<<endl;}}void Set(int r,int c){row = r; col = c;}void Input(){int i,j;for(i=0;i<row;i++)for(j=0;j<col;j++)cin>>data[i][j];}int data[N][N];int col,row;};Matrix A,B,C;int main(){int n,m,k;while(cin>>m>>n>>k &&(m||n||k)){A.Set(m,n);B.Set(n,k);A.Input();B.Input();C.Matrix_multiply(A,B);C.Output();}return 0;}

原创粉丝点击