OJ期末模考I
2015-06-28 15:08
260 查看
Problem I: P4
游戏中的Human角色
Time Limit: 1 Sec Memory Limit:
128 MB
Submit: 417 Solved:
245
[Submit][Status][Web
Board]
Description
在一个平面打斗游戏中,任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。如下程序中,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。
请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
#include <iostream>
using namespace std;
class Location
{
private:
int x, y;
public:
Location(int a, int b):x(a),y(b) {}
int getX(){return x;}
int getY(){return y;}
void setXY(int a,int b) {x=a;y=b;}; //设置位置坐标
};
class Role
{
public:
Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
void moveTo(int rx, int ry); //移动到(rx,
ty)处,要改变loc的值
void addBlood(int b); //增加血量,参数为负时,代表减少
protected:
int blood;
Location loc;
};
void Role::moveTo(int rx, int ry)
{
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
}
//************* begin *****************
class Human: public Role
{
public:
private:
string name; //
姓名
int attack; //
攻击力
};
//************* end *****************
int main()
{
string name;
int att, blood, x, y;
cin>>name>>att>>blood>>x>>y;
Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处
hum.show();
int incAtt, incBlood, newx, newy ;
cin>>incAtt;
cin>>incBlood;
cin>>newx>>newy;
hum.addAttack(incAtt); //攻击力增加incAtt
hum.addBlood(incBlood); //血量增加incBlood
hum.moveTo(newx,newy); //人移到了(news,newy)处
hum.show();
return 0;
}
Input
第一行:名字血量攻击力位置,其中位置处是两个整数,代表平面x、y坐标
第二行:增加的攻击力
第三行:要增加的血量
第四行:新位置,用两个整数代表
输入的各部分之间用空格隔开
Output
分别用两行显示游戏角色的初始状态和调整参数后的状态
如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻击力1000血量,在(4,3)位置处
Sample Input
Avanda 500 1000 4 3
-300
200
2 5
Sample Output
Avanda has 500 attack and 1000 blood in (4,3)
Avanda has 200 attack and 1200 blood in (2,5)
Submit:
游戏中的Human角色
Time Limit: 1 Sec Memory Limit:
128 MB
Submit: 417 Solved:
245
[Submit][Status][Web
Board]
Description
在一个平面打斗游戏中,任何的角色(Role)都有血量(blood)和位置loc(此处loc是Location类的实例)属性。有了Role类,可以派生出不同的角色,如人、神仙、怪兽等。如下程序中,定义了Location类和Role类,人类(Human)中新增了姓名和攻击力数据成员,请为Human类设计成员函数,并实现Role类中的moveTo和addBlood两个成员函数。
请在begin和end中间写下需要的代码。你只能编辑并提交begin和end之间的代码。
#include <iostream>
using namespace std;
class Location
{
private:
int x, y;
public:
Location(int a, int b):x(a),y(b) {}
int getX(){return x;}
int getY(){return y;}
void setXY(int a,int b) {x=a;y=b;}; //设置位置坐标
};
class Role
{
public:
Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {}
void moveTo(int rx, int ry); //移动到(rx,
ty)处,要改变loc的值
void addBlood(int b); //增加血量,参数为负时,代表减少
protected:
int blood;
Location loc;
};
void Role::moveTo(int rx, int ry)
{
loc.setXY(rx,ry);
}
void Role::addBlood(int b)
{
blood+=b;
}
//************* begin *****************
class Human: public Role
{
public:
private:
string name; //
姓名
int attack; //
攻击力
};
//************* end *****************
int main()
{
string name;
int att, blood, x, y;
cin>>name>>att>>blood>>x>>y;
Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处
hum.show();
int incAtt, incBlood, newx, newy ;
cin>>incAtt;
cin>>incBlood;
cin>>newx>>newy;
hum.addAttack(incAtt); //攻击力增加incAtt
hum.addBlood(incBlood); //血量增加incBlood
hum.moveTo(newx,newy); //人移到了(news,newy)处
hum.show();
return 0;
}
Input
第一行:名字血量攻击力位置,其中位置处是两个整数,代表平面x、y坐标
第二行:增加的攻击力
第三行:要增加的血量
第四行:新位置,用两个整数代表
输入的各部分之间用空格隔开
Output
分别用两行显示游戏角色的初始状态和调整参数后的状态
如“Avanda has 500 attack and 1000 blood in (4,3)”表示Avanda有500攻击力1000血量,在(4,3)位置处
Sample Input
Avanda 500 1000 4 3
-300
200
2 5
Sample Output
Avanda has 500 attack and 1000 blood in (4,3)
Avanda has 200 attack and 1200 blood in (2,5)
Submit:
#include <iostream> using namespace std; class Location { private: int x, y; public: Location(int a, int b):x(a),y(b) {} int getX(){return x;} int getY(){return y;} void setXY(int a,int b) {x=a;y=b;}; //设置位置坐标 }; class Role { public: Role(int rblood, int rx, int ry):blood(rblood),loc(rx,ry) {} void moveTo(int rx, int ry); //移动到(rx, ty)处,要改变loc的值 void addBlood(int b); //增加血量,参数为负时,代表减少 protected: int blood; Location loc; }; void Role::moveTo(int rx, int ry) { loc.setXY(rx,ry); } void Role::addBlood(int b) { blood+=b; } //************* begin ***************** class Human: public Role { public: Human(string nam,int att,int blood,int x,int y):Role(blood,x,y),name(nam),attack(att){} int addAttack(int n); void show(); private: string name; // 姓名 int attack; // 攻击力 }; int Human::addAttack(int n) { attack+=n; return attack; } void Human::show() { cout<<name<<" has "<<attack<<" attack and "<<blood<<" blood in "<<"("<<loc.getX()<<","<<loc.getY()<<")"<<endl; } //************* end ***************** int main() { string name; int att, blood, x, y; cin>>name>>att>>blood>>x>>y; Human hum(name,att,blood,x,y); //人name的攻击力att,血量blood,在(x,y)处 hum.show(); int incAtt, incBlood, newx, newy ; cin>>incAtt; cin>>incBlood; cin>>newx>>newy; hum.addAttack(incAtt); //攻击力增加incAtt hum.addBlood(incBlood); //血量增加incBlood hum.moveTo(newx,newy); //人移到了(news,newy)处 hum.show(); return 0; }
相关文章推荐
- 135.删除字符串中字符ASCII为奇数的数。
- [Q]pdfFactory虚拟打印机的安装
- python __str__ & __repr__ & __cmp__
- FTP服务器搭建
- Senergy.Interactive.Petrophysics.v4.2.2013.275 1CD(交互式测井分析工具)
- google chrome can not be run as root
- 使用TextRank算法为文本生成关键字和摘要
- ARM编程工具篇——交叉编译
- 入职互联网公司一个月
- VS配置路径和宏
- Redis 笔记与总结6 Redis 高级应用之 事务处理、持久化操作、pub_sub、虚拟内存
- 话说云服务器是降价,还是彻底免费?
- Schlumberger Oil Field Manager(OFM) 2012.1-ISO 1CD(油田日常监控和管理软件包)
- 深度学习系列之CNN核心内容
- asp.net 安装数据库,以及修改web.config文件
- OJ期末模考H
- 机房收费系统——组合查询
- java学习之旅35--面向对象_08_方法重载_构造方法重载
- Linux集中日志服务器rsyslog
- 根据条件设置poplist的值集