YTU 2640: 编程题:运算符重载---矩阵求和
2640: 编程题:运算符重载---矩阵求和
时间限制: 1 Sec 内存限制: 128 MB
提交: 484 解决: 190
题目描述
输入
两个2行3列矩阵
输出
矩阵之和
样例输入
1 2 3
4 5 6 7 8 9
0 1 2
样例输出
8 10 12
4 6 8
提示
只提交begin到end部分的代码
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
private:
int mat[2][3];
};
Matrix::Matrix()
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
mat[i][j]=0;
}
istream & operator>>(istream & input,Matrix & m)
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
input>>m.mat[i][j];
return input;
}
Matrix operator+(Matrix &m1,Matrix &m2)
{
Matrix m;
for(int i=0; i<2; ++i)
for(int j=0; j<3; ++j)
{
m.mat[i][j]=m1.mat[i][j]+m2.mat[i][j];
}
return m;
}
ostream& operator <<(ostream &output,Matrix &m)
{
int i,j;
for(i=0; i<2; ++i)
{
for( j=0; j<3; ++j)
{
output<<m.mat[i][j]<<" ";
}
output<<endl;
}
return output;
}
int main()
{
Matrix a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
private:
int mat[2][3];
};
Matrix::Matrix()
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
mat[i][j]=0;
}
istream & operator>>(istream & input,Matrix & m)
{
int i,j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
input>>m.mat[i][j];
return input;
}
Matrix operator+(Matrix &m1,Matrix &m2)
{
Matrix m;
for(int i=0; i<2; ++i)
for(int j=0; j<3; ++j)
{
m.mat[i][j]=m1.mat[i][j]+m2.mat[i][j];
}
return m;
}
ostream& operator <<(ostream &output,Matrix &m)
{
int i,j;
for(i=0; i<2; ++i)
{
for( j=0; j<3; ++j)
{
output<<m.mat[i][j]<<" ";
}
output<<endl;
}
return output;
}
int main()
{
Matrix a,b,c;
cin>>a;
cin>>b;
c=a+b;
cout<<c<<endl;
return 0;
}
YTU 2640: 编程题:运算符重载---矩阵求和的更多相关文章
- POJ C++程序设计 编程题#3 编程作业—运算符重载
编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 写一个二维数组 ...
- POJ C++程序设计 编程题#1 编程作业—运算符重载
编程题 #2 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的MyIn ...
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
- YTU 2597: 编程题B-选拔飞行员
2597: 编程题B-选拔飞行员 时间限制: 1 Sec 内存限制: 128 MB 提交: 131 解决: 35 题目描述 2100年空军选拔高中生飞行学员基本条件要求如下,年龄范围:16-19周 ...
- YTU 2596: 编程题B-日期格式
2596: 编程题B-日期格式 时间限制: 1 Sec 内存限制: 128 MB 提交: 981 解决: 74 题目描述 注:本题只需要提交编写的函数部分的代码即可. 将输入的日期格式 月/日/年 ...
- YTU 2858: 编程题AB-骨牌铺方格
2858: 编程题AB-骨牌铺方格 时间限制: 1 Sec 内存限制: 128 MB 提交: 87 解决: 23 题目描述 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出 ...
- YTU 2845: 编程题AB-卡片游戏
2845: 编程题AB-卡片游戏 时间限制: 1 Sec 内存限制: 128 MB 提交: 30 解决: 13 题目描述 小明对数字的序列产生了兴趣: 现有许多张不同的数字卡片,用这若干张卡片能排 ...
- YTU 2837: 编程题B-狐狸算卦
2837: 编程题B-狐狸算卦 时间限制: 1 Sec 内存限制: 128 MB 提交: 76 解决: 52 题目描述 注:本题只需要提交需要完善部分的代码,请按照C++方式提交. 小熊和狐狸是邻 ...
- YTU 2535: C++复数运算符重载(+与<<)
2535: C++复数运算符重载(+与<<) 时间限制: 1 Sec 内存限制: 128 MB 提交: 867 解决: 532 题目描述 定义一个复数类Complex,重载运算符&qu ...
随机推荐
- 洛谷——P4053 [JSOI2007]建筑抢修
P4053 [JSOI2007]建筑抢修 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的 ...
- Hibernate-02
一.hibernate实体创建规则 1.hibernate---->持久层ORM 映射框架,专注于数据的持久化工作. 2.持久化类创建规则 --->1.提供无参数的构造方法 2.私有化.对 ...
- 开发基本的php框架
github路径:https://github.com/zhengchuzhou/easyPhpFramework 一.目录结构及用途 二.相关代码: 1.入口文件(index.php): <? ...
- 简述FTP主动模式与被动模式
1 FTP工作模式 2 不同模式FTP面临的问题 3 主动模式的FTP连接建立连接主要步骤 客户端打开一个随机的端口(端口号大于1024,在这里,我们称它为x),同时一个FTP进程连接至服务器的21号 ...
- Django 模版语法 一
创建项目 django_template 和 app django-admin startproject django_template python manage.py startapp app01 ...
- LeetCode(56)Merge Intervals
题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...
- LeetCode(47)Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- Python操作Redis、Memcache
今天主要介绍如何通过python来对Redis和memcache进行操作,下面开始今天的内容: 一.Memcached操作 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web ...
- 04001_HTML简单介绍
1.超文本标记语言 (1)超文本:比普通文本功能更加强大: (2)标记语言:使用一组标签对内容进行描述的一门语言,它不是编程语言! 2.HTML语法和规范 (1)所有的html文件后缀名都是以.htm ...
- STM32F407 开发环境搭建 程序下载 个人笔记
详细资料: http://www.openedv.com/thread-13912-1-1.html 需要安装的软件: 1.keil(MDK,必选),用keygen破解 2.CH340驱动,(usb串 ...