Experiment 12: Problem G: Powerful matrix operation. problem Matrix

Source: Internet
Author: User

Experiment 12: Problem G: Powerful matrix operation. problem Matrix

Home Web Board ProblemSet Standing Status Statistics
  Problem G: Powerful matrix operation Problem G: Powerful matrix operation. Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 171 Solved: 98
[Submit] [Status] [Web Board] Description

Defines a Matrix class to store a Matrix. The + and * operators are used to calculate the sum and product of the two matrices respectively. The <and> operators are used to output and input a matrix. It is required that an Error be output when two matrices cannot be added or multiplied.

 

Input

Input 1st rows N> 0, indicating there are N groups of test cases, a total of 2N matrices.

Each group of test cases contains two matrices. Each matrix first inputs the number of rows and columns, followed by all elements of the matrix.

 

Output

Each test case generates a set of outputs. For the specific format, see the example. Note: When addition or multiplication cannot be performed, an Error should be output.

 

Sample Input32 21 11 12 22 22 21 111 22 21 112 22 22 2 Sample OutputCase 33 34 44 4 Case 2: Error2 2 Case 3: ErrorErrorHINT Append Codeappend. cc, [Submit] [Status] [Web Board]

Please refer to the following link for more information:
All Copyright Reserved 2010-2011 SDUSTOJ TEAM
GPL2.0 2003-2011 HUSTOJ Project TEAM
Anything about the Problems, Please Contact Admin: admin

#include<iostream>#define MAX 102using namespace std;class Matrix{public:    int r,c,error;    int m[MAX][MAX];    Matrix():error(0) {}    Matrix operator+(const Matrix &M)    {        Matrix tmp;        tmp.r=M.r;        tmp.c=M.c;        /*for(int i=0;i<tmp.r;i++)            for(int j=0;j<tmp.c;j++)            tmp.m[i][j]=0;*/        if(r!=M.r||c!=M.c)        {            tmp.error=1;            return tmp;        }        for(int i=0; i<r; i++)            for(int j=0; j<c; j++)            {                tmp.m[i][j]=m[i][j]+M.m[i][j];            }        return tmp;    }    Matrix operator*(const Matrix &M)    {        Matrix tmp;        tmp.r=r;        tmp.c=M.c;        for(int i=0;i<tmp.r;i++)            for(int j=0;j<tmp.c;j++)            tmp.m[i][j]=0;        if(c!=M.r)        {            tmp.error=1;            return tmp;        }        for(int i=0; i<r; i++)        {            for(int j=0; j<M.c; j++)            {                int sum = 0;                for(int k=0; k<M.r; k++)                {                    sum += m[i][k] * M.m[k][j];                }                tmp.m[i][j]  = sum;            }        }        return tmp;    }    friend istream &operator>>(istream &is,Matrix &M);    friend ostream &operator<<(ostream &os,Matrix &M);};istream &operator>>(istream &is,Matrix &M){    is>>M.r>>M.c;    for(int i=0; i<M.r; i++)        for(int j=0; j<M.c; j++)        {            is>>M.m[i][j];        }    return is;}ostream &operator<<(ostream &os,Matrix &M){    if(M.error==1)    {        os<<"Error"<<endl;        return os;    }    for(int i=0; i<M.r; i++)        for(int j=0; j<M.c; j++)        {            if(j!=M.c-1)                os<<M.m[i][j]<<" ";            else                os<<M.m[i][j]<<endl;        }    ///os<<endl;    return os;}int main(){    int cases, i;    cin>>cases;    for (i = 0; i < cases; i++)    {        Matrix A, B, C, D;        cin>>A>>B;        C = A + B;        D = A * B;        cout<<"Case "<<i + 1<<":"<<endl;        cout<<C<<endl;        cout<<D;    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.