YTU 2945: 编程:五元向量的运算
2945: 编程:五元向量的运算
时间限制: 1 Sec 内存限制: 128 MB
提交: 151 解决: 85
题目描述
用习惯了的运算符操作新定义的类对象,这是OO方法给我们带来的便利。下面要为的五元向量(每个向量有5个元素)装备加法、数乘和输入输出的运算功能,请在begin至end部分,写上需要的代码并提交。
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数,这里确定为5元向量
class MyVector
{
public:
MyVector(){}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d); //输入向量
friend ostream &operator<<(ostream &output, const MyVector &d); //输出向量
MyVector operator+(const MyVector &d); //向量相加,当前对象与d对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
//**********begin*****************
//**********end*****************
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
输入
第一个五元向量,五个数之间用空格隔开
第二个五元向量,五个数之间用空格隔开
一个整数
输出
两个五元向量的和: 格式:前后加括号,两数之间用加逗号和一个空格
第一个五元向量与整数的数乘结果,格式同前
(运算规则:和运算为向量对应位置元素相加,数乘运算为各元素分别乘以给定的数。)
样例输入
1 25 41 16 98
5 67 9 55 121
3
样例输出
+: (6, 92, 50, 71, 219)
*: (3, 75, 123, 48, 294)
你 离 开 了 , 我 的 世 界 里 只 剩 下 雨 。 。 。
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数
class MyVector
{
public:
MyVector() {}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d);
friend ostream &operator<<(ostream &output, const MyVector &d);
MyVector operator+(const MyVector &d2); //向量相加,当前对象与d2对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
istream &operator>>(istream &input, MyVector &d)
{
for(int i=0; i<5; i++)
input>>d.data[i];
return input;
}
ostream &operator<<(ostream &output, const MyVector &d)
{
int i;
output<<"("<<d.data[0]<<", ";
for(i=1; i<4; i++)
{
output<<d.data[i]<<", ";
}
output<<d.data[i]<<")";
return output;
}
MyVector MyVector::operator+(const MyVector &d)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=d.data[i]+data[i];
return a;
}
MyVector MyVector::operator*(int n)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=n*data[i];
return a;
}
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数
class MyVector
{
public:
MyVector() {}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d);
friend ostream &operator<<(ostream &output, const MyVector &d);
MyVector operator+(const MyVector &d2); //向量相加,当前对象与d2对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
istream &operator>>(istream &input, MyVector &d)
{
for(int i=0; i<5; i++)
input>>d.data[i];
return input;
}
ostream &operator<<(ostream &output, const MyVector &d)
{
int i;
output<<"("<<d.data[0]<<", ";
for(i=1; i<4; i++)
{
output<<d.data[i]<<", ";
}
output<<d.data[i]<<")";
return output;
}
MyVector MyVector::operator+(const MyVector &d)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=d.data[i]+data[i];
return a;
}
MyVector MyVector::operator*(int n)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=n*data[i];
return a;
}
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
YTU 2945: 编程:五元向量的运算的更多相关文章
- YTU 2611: A代码完善--向量的运算
2611: A代码完善--向量的运算 时间限制: 1 Sec 内存限制: 128 MB 提交: 256 解决: 168 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 对于二维 ...
- R语言编程艺术# 数据类型向量(vector)
R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用t ...
- Linux网络编程(五)
/*Linux网络编程(五)——多路IO复用之select() 网络编程中,使用IO复用的典型场合: 1.当客户处理多个描述字时(交互式输入以及网络接口),必须使用IO复用. 2.一个客户同时处理多个 ...
- C++模板元编程 - 1 基本数据类型和运算
这是博客开通前几天做的,C++的模板没办法存方便的浮点数,算了. 基本类型的设计参考了vczh轮子叔模仿boost的MPL的设计. 话说template和typename写多了真是无限烦人啊,不得已定 ...
- R语言入门:向量的运算
向量之间的加减乘除运算: > x <- 1 > x [1] 1 2 3 4 5 6 7 8 9 10 > x=x+1 > x [1] 2 3 4 5 6 7 8 9 10 ...
- socket编程五种模型
客户端:创建套接字,连接服务器,然后不停的发送和接收数据. 比较容易想到的一种服务器模型就是采用一个主线程,负责监听客户端的连接请求,当接收到某个客户端的连接请求后,创建一个专门用于和该客户端通信的套 ...
- python高级编程之元类(第3部分结束)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...
- C++面向对象高级编程(五)类与类之间的关系
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming OOP面向对象编 ...
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
随机推荐
- C. The Smallest String Concatenation-C++sort排序~~
C. The Smallest String Concatenation time limit per test 3 seconds memory limit per test 256 megabyt ...
- 【bzoj4260】 Codechef REBXOR trie树
Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample Input ...
- 按WSDL信息手动生成代理类
命令行: wsdl /language:c# /n:Entity /out:C:\Users\mengxianming\Desktop\Centrex_IMS_Client.cs C:\Users\m ...
- 【BZOJ4475】子集选取(计数)
题意: 思路: #include<cstdio> #include<cstdlib> #include<iostream> #include<algorith ...
- Codeforces Round #295 D. Cubes [贪心 set map]
传送门 D. Cubes time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Unique Paths II (dp题)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Bzoj4540][Hnoi2016] 序列(莫队 + ST表 + 单调队列)
4540: [Hnoi2016]序列 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1567 Solved: 718[Submit][Status] ...
- Codechef-BLACKCOM(树形背包dp)
题意: 给你一棵由 N 个节点构成的树 T.节点按照 1 到 N 编号,每个节点要么是白色,要么是黑色.有 Q 组询问,每组询问形如 (s, b).你需要检查是否存在一个连通子图,其大小恰好是 s,并 ...
- 简论远程通信(RPC,Webservice,RMI,JMS的区别)
RPC(Remote Procedure Call Protocol)RPC使用C/S方式,采用http协议,发送请求到服务器,等待服务器返回结果.这个请求包括一个参数集和一个文本集,通常形成“cla ...
- http://www.doframe.com/jetoolweb/index.html
http://www.doframe.com/jetoolweb/index.html http://www.doframe.com/jetoolweb/html/tasks/orders.html# ...