代码填充--雨昕学矩阵

2020年1月17日 710点热度 0人点赞 0条评论

代码填充--雨昕学矩阵

时间: 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

提示:

参考答案:

解锁文章

没有看到答案?微信扫描二维码可免费解锁文章

微信扫描二维码解锁

使用微信扫描二维码打开广告页面后可以立即关闭,再刷新此页面即可正常浏览此文章

所跳转广告均由第三方提供,并不代表本站观点!

已经扫描此二维码?点此立即跳转

code

这个人很懒,什么都没留下

文章评论