- This topic is mainly the overloading of multiplication operators, card me for a long time, matrix multiplication with 3 nested for loop, to divide the matrix of the multiplication result is the first matrix of the row, the second matrix of the column composed of the Matrix.
- When you overload the +,* operator, you can pass two matrix references in the argument list, representing the matrix before and after the operation, or the matrix reference after the only operator , using the implied matrix that the this pointer points to. I'm using the latter.
Home |
Web Board |
Problemset |
Standing |
Status |
Statistics |
Problem G: A powerful matrix operation comes Problem G: Powerful matrix operations come time Limit: 1 Sec Memory Limit: MB
Submit: 171 Solved: 98
[Submit] [Status] [Web Board] Description
Defines a matrix class that is used to store an array. Overloading its +, * operators, respectively, for calculating the sum and product of two matrices, and overloading its << and >> operators for outputting and entering a matrix. Requires that when two matrices cannot be additive or multiplicative, error should be output.
Input
Enter line 1th n>0, which indicates that there are n sets of test cases with a total of 2N matrices.
Each set of test cases consists of 2 matrices. Each matrix first enters the number of rows, columns, and then all the elements of the matrix.
Output
Each test case produces a set of outputs. The specific format is shown in the sample. Note: Error should be output when addition or multiplication is not possible.
Sample Input2 of 111 in the same.Sample OutputCase 1:3 4Case 2:error2 2Case 3:errorerrorHINT Append codeappend.cc, [Submit] [Status] [Web Board]
??? < Chinese????? 中文版???
All Copyright Reserved 2010-2011 sdustoj TEAM
GPL2.0 2003-2011 hustoj Project TEAM
Anything about the problems admin:admin
#include <iostream>#defineMAX 102using namespacestd;classmatrix{ Public: intR,c,error; intM[max][max]; Matrix (): Error (0) {} Matrixoperator+(ConstMatrix &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; returntmp; } for(intI=0; i<r; i++) for(intj=0; j<c; J + +) {Tmp.m[i][j]=m[i][j]+M.m[i][j]; } returntmp; } Matrixoperator*(ConstMatrix &M) {Matrix tmp; TMP.R=R; TMP.C=M.C; for(intI=0; i<tmp.r;i++) for(intj=0; j<tmp.c;j++) Tmp.m[i][j]=0; if(c!=M.R) {tmp.error=1; returntmp; } for(intI=0; i<r; i++) { for(intj=0; j<m.c; J + +) { intsum =0; for(intk=0; k<m.r; k++) {sum+ = m[i][k] *M.m[k][j]; } Tmp.m[i][j]=sum; } } returntmp; } Friend IStream&operator>> (IStream & is, Matrix &l); Friend Ostream&operator<< (ostream &os,matrix &M);}; IStream&operator>> (IStream & is, Matrix &M) { is>>M.r>>M.C; for(intI=0; i<m.r; i++) for(intj=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; returnOS; } for(intI=0; i<m.r; i++) for(intj=0; j<m.c; J + +) { if(j!=m.c-1) OS<<M.m[i][j]<<" "; ElseOS<<M.m[i][j]<<Endl; } ///OS<<endl;return OS;}intMain () {intcases, I; CIN>>cases; for(i =0; i < cases; i++) {Matrix A, B, C, D; CIN>>A>>C; C= A +B; D= A *B; cout<<" Case"<<i +1<<":"<<Endl; cout<<C<<Endl; cout<<D; } return 0;}
Experiment 12:problem G: A powerful matrix operation comes