• YTU 2959: 代码填充--雨昕学矩阵


    2959: 代码填充--雨昕学矩阵

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 112  解决: 50

    题目描述

    雨昕开始学矩阵了。矩阵数乘规则:一个数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

    你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

    #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;
    }
    istream& operator>>(istream &a,Matrix &b)
    {
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<3; j++)
                cin>>b.mat[i][j];
        }
    }
    Matrix operator*(int k,Matrix &p)
    {
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<3; j++)
                p.mat[i][j]*=k;
        }
        return p;
    }
    int main()
    {
        Matrix mat1,mat2;
        int k;
        cin>>k;
        cin>>mat1;
        mat2=k*mat1;
        cout<<mat2<<endl;
        return 0;
    }
    

  • 相关阅读:
    LocalImprove算法
    Improve算法
    CSU-ACM2014年校队选拔赛指导赛解题报告
    CSU-ACM暑假集训基础组训练赛(4)解题报告
    CSU-ACM暑假集训基础组七夕专场
    CSU-ACM暑假集训基础组训练赛(2) 解题报告
    CSU-ACM2014暑假集训基础组训练赛(1) 解题报告
    Aizu 2164 CSUOJ 1436 Revenge of the Round Table
    插头DP小结
    Codeforces 128C Games with Rectangle
  • 原文地址:https://www.cnblogs.com/im0qianqian/p/5989638.html
Copyright © 2020-2023  润新知