Problem C: 强悍的矩阵运算来了

Problem C: 强悍的矩阵运算来了

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1178   Solved: 750
[ Submit][ Status][ Web Board]

Description

定义一个Matrix类,用于存储一个矩阵。重载其+、*运算符,分别用于计算两个矩阵的和、乘积;重载其<<和>>运算符,用于输出和输入一个矩阵。要求当两个矩阵不能进行加法或乘法运算时,应该输出Error。

Input

输入第1行N>0,表示有N组测试用例,共2N个矩阵。

每组测试用例包括2个矩阵。每个矩阵首先输入行数、列数,之后是该矩阵的所有元素。

Output

每个测试用例产生一组输出。具体格式见样例。注意:当不能进行加法或乘法运算时,应输出Error。

Sample Input

32 21 11 12 22 22 21 111 22 21 112 22 22 2

Sample Output

Case 1:3 33 34 44 4Case 2:Error2 2Case 3:ErrorError

HINT

Append Code

append.cc

#include<bits/stdc++.h>
using namespace std;
class Matrix
{
private:
    int hang,lie;
    bool flag;
    int **ppp;
public:
    friend istream& operator>>(istream& in,Matrix & abc)
    {
        in>>abc.hang>>abc.lie;
        abc.ppp = new int*[abc.hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i] = new int[abc.lie];
        for(int i=0;i<abc.hang;i++)
        {
            for(int j=0;j<abc.lie;j++)
            {
                in>>abc.ppp[i][j];
            }
        }
        return in;
    }
    friend ostream& operator<<(ostream& ou,Matrix & abc)
    {
        if(abc.flag == false){
                ou<<"Error"<<endl;
                return ou;
        }
        for(int i = 0;i<abc.hang;i++)
        {
            for(int j=0;j<abc.lie;j++)
            {
                if(j==0)
                ou<<abc.ppp[i][j];
                else ou<<" "<<abc.ppp[i][j];
            }
            ou<<endl;
        }
       // ou<<endl;
        return ou;
    }
    Matrix operator+(Matrix &another)
    {
        Matrix abc;
        abc.flag = true;
        if(another.hang != hang||another.lie != lie)
        {
            abc.flag=false;
            return abc;
        }
        abc.hang = hang;
        abc.lie = lie;
        abc.ppp = new int*[hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i] = new int[lie];
        for(int i=0;i<hang;i++)
        {
            for(int j=0;j<lie;j++)
            {
                abc.ppp[i][j] = ppp[i][j]+another.ppp[i][j];
            }
        }
        return abc;
    }
    Matrix operator*(Matrix &another)
    {
         Matrix abc;
        abc.flag = true;
        if(another.hang != lie)
        {
            abc.flag=false;
            return abc;
        }
        abc.hang = hang;
        abc.lie = another.lie;
        abc.ppp = new int*[hang];
        for(int i=0;i<abc.hang;i++)
        abc.ppp[i] = new int[another.lie];
        for(int i=0;i<hang;i++)
        {
            for(int j=0;j<another.lie;j++)
            {
                abc.ppp[i][j] =0;
                for(int k=0;k<lie;k++)
                    abc.ppp[i][j]+=another.ppp[k][j]*ppp[i][k];
            }
        }
        return abc;
    }
};
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;
}
/**************************************************************
    Problem: 1816
    User: 201701060928
    Language: C++
    Result: Accepted
    Time:8 ms
    Memory:1268 kb
****************************************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值