求矩阵之和
来源:互联网 发布:网络耗材清单 编辑:程序博客网 时间:2024/05/16 08:53
1.题目:
Problem Description
设计一个矩阵类Data,包括矩阵的行、列、矩阵数据等私有数据成员。要求实现两个矩阵的加。
Input
输入数据有多组,每组输入数据有三行,第一行两个整数,分别代表矩阵的行数m和列数n;第二行有m*n个整数,是第一个矩阵的数据;第三行也有m*n个整数,是第二个矩阵的数据。
Output
对于每组输入数据,输出两个矩阵的和。
Sample Input
3 31 1 1 2 2 2 3 3 31 1 1 1 1 1 1 1 12 31 2 3 4 5 61 1 1 1 1 1
Sample Output
2 2 23 3 34 4 42 3 45 6 7
2.下面这个代码没有真正达到目的:
#include <iostream>using namespace std;#define N 500class Data{ private: int row, col, *a; public: Data(); Data(int* b, int& l, int& r); Data operator+(Data&); void show(); };Data::Data(){ row = 0; col = 0; int* b = new int[N]; a = b;}Data::Data(int* b, int& l, int& r){ row = l; col = r; a = b;}Data Data::operator+(Data& c){ Data sum; sum.row = c.row; sum.col = c.col; for (int i = 0; i < row * col; i++) sum.a[i] = a[i] + c.a[i]; return sum;}void Data::show(){ int i, j, k = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (j == 0) cout << a[k++]; else cout << ' ' << a[k++]; } cout << endl; }}int main(){ int m, n, i, j, a[N], b[N]; while (cin >> m >> n) { for (i = 0; i < m * n; i++) cin >> a[i]; for (j = 0; j < m * n; j++) cin >> b[j]; Data x(a, m, n), y(b, m, n), z; z = x + y; z.show(); } return 0;}
看看现在这段代码:
#include <iostream>//#include <stdio.h>using namespace std;class Matrix{ const int row, col; int* a;public: Matrix(int r, int c); Matrix(Matrix& m); ~Matrix(); Matrix& operator=(Matrix& m); Matrix operator+(Matrix& m); friend istream& operator>>(istream& in, Matrix& m); friend ostream& operator<<(ostream& out, Matrix& m);};Matrix::Matrix(int r, int c): col(c), row(r){ int i; a = new int[r * c]; for (i = 0; i < r * c; i++) a[i] = 0;}Matrix::Matrix(Matrix& m): col(m.col), row(m.row){ int i; a = new int[col * row]; for (i = 0; i < col * row; i++) a[i] = m.a[i];}Matrix::~Matrix(){ delete []a;}Matrix& Matrix::operator=(Matrix& m){ int i; if (m.row == row && m.col == col) for (i = 0; i < row * col; i++) a[i] = m.a[i]; return *this;}Matrix Matrix::operator+(Matrix& m){ Matrix b(m.row, m.col); int i; for (i = 0; i < row * col; i++) b.a[i] = m.a[i] + a[i]; return b;}istream& operator>>(istream& in, Matrix& m){ int i; for (i = 0; i < m.row * m.col; i++) { cin >> m.a[i]; } return in;}ostream& operator<<(ostream& out, Matrix& m){ int i, j; for (i = 0; i < m.row; i++) { for (j = 0; j < m.col; j++) { if (j == 0) cout << m.a[i * m.col + j]; else cout << " " << m.a[i * m.col + j]; } cout << endl; } return out;}int main(){// freopen("1.txt", "r", stdin); int a, b; while (cin >> a >> b) { Matrix x(a, b); cin >> x; Matrix y(a, b); cin >> y; cout << x + y; } return 0;}
- 求矩阵之和
- 求矩阵之和c++
- 求矩阵的对角线之和
- 求矩阵对角线元素之和
- 求矩阵对角线元素之和
- 求两个对称矩阵之和与乘积
- c++求3x3矩阵 对角线之和
- OJ--求矩阵对角线元素之和
- oj_求矩阵对角线元素之和
- 求3×3矩阵对角线元素之和
- 求两个矩阵之和(重载)
- 求3x3矩阵对角线之和
- 求3*3矩阵对角线元素之和
- 求3×3矩阵对角线元素之和
- 求3×3矩阵对角线元素之和
- 求3×3矩阵对角线元素之和
- 求输入矩阵的对角线之和
- 7-4 求矩阵各行元素之和
- PHP一书开始于今夜,努力改变自己。逍遥、平静的度过一生
- VS2010快捷键设置及修改
- 20130513 做banner遇到的问题
- jdk源码学习------String
- 国内3d视频游戏领域研究
- 求矩阵之和
- 一个activity中多个handler和消息的处理过程
- java静态代理和动态代理
- 整除的尾数 tzc
- micro SD card 在 android上的性能对比,后面还有IOS的I/O性能对比
- SGU 106 The euqation
- 命令行调用svn
- 我的学习生涯(Delphi篇) - 03
- Transact-SQL编程规范