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]

  1. #include<iostream>
  2. #define MAX 102
  3. using namespace std;
  4. class Matrix
  5. {
  6. public:
  7. int r,c,error;
  8. int m[MAX][MAX];
  9. Matrix():error() {}
  10. Matrix operator+(const Matrix &M)
  11. {
  12. Matrix tmp;
  13. tmp.r=M.r;
  14. tmp.c=M.c;
  15. /*for(int i=0;i<tmp.r;i++)
  16. for(int j=0;j<tmp.c;j++)
  17. tmp.m[i][j]=0;*/
  18. if(r!=M.r||c!=M.c)
  19. {
  20. tmp.error=;
  21. return tmp;
  22. }
  23. for(int i=; i<r; i++)
  24. for(int j=; j<c; j++)
  25. {
  26. tmp.m[i][j]=m[i][j]+M.m[i][j];
  27. }
  28. return tmp;
  29. }
  30. Matrix operator*(const Matrix &M)
  31. {
  32. Matrix tmp;
  33. tmp.r=r;
  34. tmp.c=M.c;
  35. for(int i=;i<tmp.r;i++)
  36. for(int j=;j<tmp.c;j++)
  37. tmp.m[i][j]=;
  38. if(c!=M.r)
  39. {
  40. tmp.error=;
  41. return tmp;
  42. }
  43. for(int i=; i<r; i++)
  44. {
  45. for(int j=; j<M.c; j++)
  46. {
  47. int sum = ;
  48. for(int k=; k<M.r; k++)
  49. {
  50. sum += m[i][k] * M.m[k][j];
  51. }
  52. tmp.m[i][j] = sum;
  53. }
  54. }
  55. return tmp;
  56. }
  57. friend istream &operator>>(istream &is,Matrix &M);
  58. friend ostream &operator<<(ostream &os,Matrix &M);
  59.  
  60. };
  61. istream &operator>>(istream &is,Matrix &M)
  62. {
  63. is>>M.r>>M.c;
  64. for(int i=; i<M.r; i++)
  65. for(int j=; j<M.c; j++)
  66. {
  67. is>>M.m[i][j];
  68. }
  69. return is;
  70. }
  71. ostream &operator<<(ostream &os,Matrix &M)
  72. {
  73. if(M.error==)
  74. {
  75. os<<"Error"<<endl;
  76. return os;
  77. }
  78. for(int i=; i<M.r; i++)
  79. for(int j=; j<M.c; j++)
  80. {
  81. if(j!=M.c-)
  82. os<<M.m[i][j]<<" ";
  83. else
  84. os<<M.m[i][j]<<endl;
  85. }
  86. ///os<<endl;
  87. return os;
  88. }
  89. int main()
  90. {
  91. int cases, i;
  92. cin>>cases;
  93. for (i = ; i < cases; i++)
  94. {
  95. Matrix A, B, C, D;
  96. cin>>A>>B;
  97. C = A + B;
  98. D = A * B;
  99. cout<<"Case "<<i + <<":"<<endl;
  100. cout<<C<<endl;
  101. cout<<D;
  102. }
  103. return ;
  104. }

实验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. 安卓开发笔记——自定义HorizontalScrollView控件(实现QQ5.0侧滑效果)

    对于滑动菜单栏SlidingMenu,大家应该都不陌生,在市场上的一些APP应用里经常可以见到,比如人人网,FaceBook等. 前段时间QQ5.0版本出来后也采用了这种设计风格:(下面是效果图) 之 ...

  2. IOS中多版本,多设备类型支持注意事项

    IOS系统从07年出来,到现在也有6年了,每年发布一次到两次新的设备,从iPhone1,iPhone2 ... iPhone4s再到最新的iPhone5.硬件在升级的过程中CPU的架构也可能发生变化, ...

  3. Arcgis10.2 破解注意问题

    Arcgis10.2 破解注意问题 2014年11月17日 20:22 按照网上教程直接替换service.txt和arcgis.exe之后 需要重新启动license服务 可是无法启动 解决办法 修 ...

  4. 【迁移学习】2010-A Survey on Transfer Learning

    资源:http://www.cse.ust.hk/TL/ 简介: 一个例子: 关于照片的情感分析. 源:比如你之前已经搜集了大量N种类型物品的图片进行了大量的人工标记(label),耗费了巨大的人力物 ...

  5. 微信公众号开发第六课 BAE结合实现迅雷账号随机分享

    迅雷离线是个好东西,那么我们能不能实现这样一个功能,回复迅雷,随机返回一个迅雷账户和密码. 首先在t_type类型表中添加 迅雷以及对应用值xunlei,这样返回的case值中对应值xunlei. 建 ...

  6. DDD:再谈:实体能否处于非法状态?

    背景 实体能否处于非法状态吗?如果实体只承担其作为实体的职责,我不认为实体可以处于非法状态,如果您将实体在不同的分层之间传递,如:UI->Application->Domain-Data, ...

  7. 用cart(分类回归树)作为弱分类器实现adaboost

    在之前的决策树到集成学习里我们说了决策树和集成学习的基本概念(用了adaboost昨晚集成学习的例子),其后我们分别学习了决策树分类原理和adaboost原理和实现, 上两篇我们学习了cart(决策分 ...

  8. 用Qt写软件系列二:QCookieViewer(浏览器Cookie查看器)

    预备 继上篇<浏览器缓存查看器QCacheViewer>之后,本篇开始QCookieViewer的编写.Cookie技术作为网站收集用户隐私信息.分析用户偏好的一种手段,广泛应用于各大网站 ...

  9. vs2015 Android SDK

    It was not possible to complete an automatic installation. This might be due to a problem with your ...

  10. 【循序渐进学Python】5.Python常用流程控制及其他语句

    1. 赋值语句常用技巧 通过序列解包(sequence unpacking)可以将多个值的序列解开,让后一一放置到变量的序列中.解包的序列中的元素必须和等号左边变量数量一致.如下: values = ...