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 ...
随机推荐
- 计算几何——认识基本object:点、线、面 。
认识基本object:点.线.面 一.点 点用P(x, y)来表示:如: typedef pair<double, double> _pair; _pair point[MAXN]; 二 ...
- while(n--)
while(n--)的意思:先判断n是否等于0,如果等于0,就不循环.如果不等于0,就进入循环,同时n的值减1.一直等到n=0才退出while循环. C语言.C++
- 分布式集群算法 memcached 如何实现分布式?
memcached 是一个”分布式缓存”,然后 memcached 并不像 mongoDB 那 样,允许配置多个节点,且节点之间”自动分配数据”. 就是说--memcached 节点之间,是不互相通信 ...
- 常用的四种设计模式 PHP代码
// 工厂模式 interface Iuser { public function getUserName(); } class UserFactory { static public functio ...
- LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Mysql篇
文章来源:http://www.cnblogs.com/hello-tl/p/7569097.html 更新时间:2017-09-21 16:06 简介 LAMP+R指Linux+Apache+Mys ...
- Python之机器学习-sklearn生成随机数据
sklearn-生成随机数据 import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotli ...
- PID28 [Stupid]愚蠢的宠物
题链:https://www.rqnoj.cn/problem/28 题目描述 背景 大家都知道,sheep有两只可爱的宠物(一只叫神牛,一只叫神菜).有一天,sheep带着两只宠物到狗狗家时,这两只 ...
- JQuery常用的案例
1.给导航栏添加鼠标移上去的时候变换背景颜色的方法. $(function () { $(".nav li").mouseover(function () { $(this).cs ...
- 【转】jquery 注册事件的方法
原文链接:http://outofmemory.cn/code-snippet/2123/jquery-zhuce-event-method 1.使用事件名来绑定,可用的事件名有 change,cli ...
- ABC077翻车实况
今天强行打一波ABC,想作为信心赛,然而= = T1 日常练习读入&输出 T2 $STL$大法好,$sqrt$保平安,我强行递推$WA$了一圈,然后罚时++ T3 woc好难啊,$n=1 ...