沐沐Mnn 2019-04-28 11:53 采纳率: 72.7%
浏览 1257
已采纳

C++编程填空:矩阵排序

描述

创建矩阵类,要求能够输入整数类型的m*n矩阵,并按照元素个数,矩阵中元素之和,创建矩阵顺序对矩阵类分别排序

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Mat{
    int h,w;
public:
    Mat(int height,int width):h(height),w(width)
// 在此处补充你的代码
int main()
{
    vector<Mat> m;
    m.push_back(Mat(2,2));
    m.push_back(Mat(3,4));
    m.push_back(Mat(2,2));
    cin >> m[0] >> m[1] >> m[2];
    sort(m.begin(),m.end());
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_1);
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    cout<<"*************"<<endl;
    sort(m.begin(),m.end(),comparator_2());
    cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
    return 0;
}

输入

前两行是一个2*2矩阵

之后三行是一个3*4矩阵

最后两行是一个2*2矩阵

输出

先按照元素个数从小到大输出三个矩阵(大小相同时后创建的矩阵先输出)

再按照元素之和从小到大输出三个矩阵(大小相同时后创建的矩阵先输出)

再按照矩阵创建顺序从先到后输出三个矩阵

(矩阵排列方式与输入相同,每个元素后用一个空格进行分隔)

样例输入

2 3
3 4
 0 12 -3 -4
-2  2 -1  0
-1 -1 -1 -1
-1 3
-2 4

样例输出

-1 3
-2 4

2 3
3 4

0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

*************
0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

-1 3
-2 4

2 3
3 4

*************
2 3
3 4

0 12 -3 -4
-2 2 -1 0
-1 -1 -1 -1

-1 3
-2 4
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-04-28 12:49
    关注
    // Q759043.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    class Mat{
        int h,w;
    public:
        Mat(int height,int width):h(height),w(width)
    // 在此处补充你的代码
        {
            p = new int[h * w];
            id = ++cnt;
        }
        int *p;
        int id;
        friend istream& operator>> (istream&, Mat&);
        friend ostream& operator<< (ostream&, Mat&);
        friend bool comparator_1(Mat a, Mat b);
        friend bool comparator_2(Mat a, Mat b);
        bool operator< (const Mat& other) const 
        {
            return h * w < other.h * other.w;
        }
        static int cnt;
    };
    
    int Mat::cnt = 0;
    
    istream& operator >> (istream& input, Mat& m)
    {
        for (int i = 0; i < m.h * m.w; i++)
            input >> m.p[i];
        return input;
    }
    
    ostream& operator<<(ostream& output, Mat& m)
    {
        for (int i = 0; i < m.h; i++)
        {
            for (int j = 0; j < m.w; j++)
                output << m.p[i * m.w + j] << " ";
            output << endl;
        }
        return output;
    }
    
    bool comparator_1(Mat a, Mat b) {
        int sum1 = 0;
        for (int i = 0; i < a.h * a.w; i++)
            sum1 += a.p[i];
        int sum2 = 0;
        for (int i = 0; i < b.h * b.w; i++)
            sum2 += b.p[i];
        return sum1 < sum2;
    }
    
    bool comparator_2(Mat a, Mat b) {
        return a.id < b.id;
    }
    
    int main()
    {
        vector<Mat> m;
        m.push_back(Mat(2,2));
        m.push_back(Mat(3,4));
        m.push_back(Mat(2,2));
        cin >> m[0] >> m[1] >> m[2];
        sort(m.begin(),m.end());
        cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
        cout<<"*************"<<endl;
        sort(m.begin(),m.end(),comparator_1);
        cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
        cout<<"*************"<<endl;
        sort(m.begin(),m.end(),comparator_2);
        cout<< m[0] << endl << m[1] << endl << m[2] <<endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: