代码填充--雨昕学矩阵
时间: 1ms 内存:128M
描述:
雨昕开始学矩阵了。矩阵数乘规则:一个数k乘一个矩阵A还是一个矩阵,行数、列数不变,矩阵的所有元素都乘以k。现在老师要求雨昕写出一个数乘以一个2*3的矩阵的完整程序。请帮助雨昕。
注:本题只需要提交填写部分的代码,请按照C++方式提交。
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator*(int k,Matrix &);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
private:
int mat[2][3];
};
Matrix::Matrix()
{
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
mat[i][j]=0;
}
ostream& operator<<(ostream &out,Matrix &mat)
{
for (int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
if(j>0) out<<" ";
out<<mat.mat[i][j];
}
out<<endl;
}
return out;
}
/*请在该部分补充缺少的代码
*/int main()
{
Matrix mat1,mat2;
int k;
cin>>k;
cin>>mat1;
mat2=k*mat1;
cout<<mat2<<endl;
return 0;
}
输入:
第一行是一个整数k
第二行开始是一个2*3的矩阵
输出:
矩阵的数乘结果
示例输入:
2
1 2 3
4 5 6
示例输出:
2 4 6
8 10 12
提示:
参考答案:
文章评论