1. 这个题目主要是乘法运算符的重载,卡了我好久,矩阵的乘法用3个嵌套的for循环进行,要分清楚矩阵的乘法结果是第一个矩阵的行,第二个矩阵的列所组成的矩阵。
  2. 重载+,*运算符时,可以在参数列表中传两个矩阵引用,分别表示前后进行运算的矩阵,或者是只传运算符之后的矩阵引用,前一个矩阵用的是隐含的this指针指向的矩阵。我用的是后者。
Home Web Board ProblemSet Standing Status Statistics
 

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

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

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 171  Solved: 98
[Submit][Status][Web Board]

Description

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

Input

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

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

Output

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

Sample Input

3
2 2
1 1
1 1
2 2
2 2
2 2
1 1
1
1 2
2 2
1 1
1
2 2
2 2
2 2

Sample Output

Case 1:
3 3
3 3

4 4
4 4
Case 2:
Error

2 2
Case 3:
Error

Error

HINT

 

Append Code

[Submit][Status][Web Board]

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

实验12:Problem G: 强悍的矩阵运算来了的更多相关文章

  1. 实验9:Problem G: 克隆人来了!

    想要输出""的话: cout<<"A person whose name is \""<<name<<" ...

  2. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem G: Check The Check(模拟国际象棋)

    Problem G: Check The Check Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 3[Submit][Statu ...

  3. The Ninth Hunan Collegiate Programming Contest (2013) Problem G

    Problem G Good Teacher I want to be a good teacher, so at least I need to remember all the student n ...

  4. 【贪心+中位数】【新生赛3 1007题】 Problem G (K)

    Problem G Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  5. Problem G: If We Were a Child Again

    Problem G: If We Were a Child AgainTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 14[Submi ...

  6. Problem G: Keywords Search

    Problem G: Keywords SearchTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 10 Solved: 6[Submit][Status] ...

  7. BZOJ4977 八月月赛 Problem G 跳伞求生 set 贪心

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4977 - 八月月赛 Problem G 题意 小明组建了一支由n名玩家组成的战队,编号依次为1到n ...

  8. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp

    Problem G. k-palindrome 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7022 ...

  9. ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)

    题目链接  ZOJ Monthly, March 2018 Problem G 题意  给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...

随机推荐

  1. Django Push HTTP Response to users

    Django Push HTTP Response to users I currently have a very simple web application written in Django, ...

  2. K3 WISE开发手册

    1.VB插件工程的命名.命名空间和生成的DLL命名要一致,否则导致注册不成功!

  3. 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限

    大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...

  4. Linux文件查找工具之find “大宝剑”--转载

    原文地址:http://xinzong.blog.51cto.com/10018904/1749465 一.文件查找工具常用软件 locate: locate命令其实是find -name的另一种写法 ...

  5. Android 学习笔记之如何使用SQLite数据库来保存数据...

    PS:最近一阵子都在为考试复习...坑爹的计算机网络,复习了3天,最后该不会的还是不会...明天还考英语...真蛋疼... 学习内容: 1.使用SQLite数据库来保存数据... SQLite:   ...

  6. Java中基本数据类型的存储方式和相关内存的处理方式(java程序员必读经典)

    1.java是如何管理内存的 java的内存管理就是对象的分配和释放问题.(其中包括两部分) 分配:内存的分配是由程序完成的,程序员需要通过关键字new为每个对象申请内存空间(基本类型除外),所有的对 ...

  7. Action接收页面传来的参数方法

    接收页面传来的参数方法 1.第一种:在action中设置相应的变量 在相应的action中设置与将要传进来的参数名相同的变量 eg: 页面传给后台两个参数 name=chance & age ...

  8. Blocked Process Report

    当同个对象上有互斥的锁存在时,查询需要等待很长时间,我们是否可以收到来自SQL Server提醒?答案是可以的,做法非常简单,因为SQL Server为你提供了称为Blocked Process Re ...

  9. Winform自定义窗体样式,实现标题栏可灵活自定义

    最近在编写C/S结构应用程序时,感觉窗体的标题栏样式太死板了,标题文字不能更改大小.颜色.字体等,按钮不能隐藏等问题,在网上也查找了许多相关的资料,没有找到合适的解决方案,发现许多人也在寻求这个问题, ...

  10. js代码中的闭包

    作为一个后台开发人员了解前端非常重要,尤其是深处学校实验室做项目时前端把写好的代码直接给你,然后你在修改的时候.我经常做的就是修改前端的代码的HTML和后台交互的部分以及js的ajax部分,之后修改之 ...