编程题#2: 魔兽世界之二:装备

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。

红司令部,City 1,City 2,……,City n,蓝司令部

两军的司令部都会制造武士。武士一共有 dragon 、ninja、iceman、lion、wolf 五种。每种武士都有编号、生命值这两种属性。

有的武士可以拥有武器。武器有三种,sword, bomb,和arrow,编号分别为0,1,2。

双方的武士编号都是从1开始计算。红方制造出来的第 n 个武士,编号就是n。同样,蓝方制造出来的第 n 个武士,编号也是n。

不同的武士有不同的特点。

dragon 可以拥有一件武器。编号为n的dragon降生时即获得编号为 n%3 的武器。dragon还有“士气”这个属性,是个浮点数,其值为它降生后其司令部剩余生命元的数量除以造dragon所需的生命元数量。

ninjia可以拥有两件武器。编号为n的ninjia降生时即获得编号为 n%3 和 (n+1)%3的武器。

iceman有一件武器。编号为n的iceman降生时即获得编号为 n%3 的武器。

lion 有“忠诚度”这个属性,其值等于它降生后其司令部剩余生命元的数目。

wolf没特点。

请注意,在以后的题目里,武士的士气,生命值,忠诚度在其生存期间都可能发生变化,都有作用,武士手中的武器随着使用攻击力也会发生变化。

武士在刚降生的时候有一个生命值。

在每个整点,双方的司令部中各有一个武士降生。

红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序循环制造武士。

蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序循环制造武士。

制造武士需要生命元。

制造一个初始生命值为 m 的武士,司令部中的生命元就要减少 m 个。

如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。

给定一个时间,和双方司令部的初始生命元数目,要求你将从0点0分开始到双方司令部停止制造武士为止的所有事件按顺序输出。

一共有两种事件,其对应的输出样例如下:

1) 武士降生

输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter

表示在 4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。

如果造出的是dragon,那么还要输出一行,例:

It has a arrow,and it’s morale is 23.34

表示该dragon降生时得到了arrow,其士气是23.34(为简单起见,本题中arrow前面的冠词用a,不用an,士气精确到小数点后面2位,四舍五入)

如果造出的是ninjia,那么还要输出一行,例:

It has a bomb and a arrow

表示该ninjia降生时得到了bomb和arrow。

如果造出的是iceman,那么还要输出一行,例:

It has a sword

表示该iceman降生时得到了sword。

如果造出的是lion,那么还要输出一行,例:

It’s loyalty is 24

表示该lion降生时的忠诚度是24。

2) 司令部停止制造武士

输出样例: 010 red headquarter stops making warriors

表示在 10点整,红方司令部停止制造武士

输出事件时:

首先按时间顺序输出;

同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。

输入

第一行是一个整数,代表测试数据组数。

每组测试数据共两行。

第一行,一个整数M。其含义为: 每个司令部一开始都有M个生命元( 1 <= M <= 10000)

第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000

输出

对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。

对每组测试数据,首先输出“Case:n” n是测试数据的编号,从1开始

接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。

样例输入

1
20
3 4 5 6 7
样例输出

Case:1
000 red iceman 1 born with strength 5,1 iceman in red headquarter
It has a bomb
000 blue lion 1 born with strength 6,1 lion in blue headquarter
It’s loyalty is 14
001 red lion 2 born with strength 6,1 lion in red headquarter
It’s loyalty is 9
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
It has a arrow,and it’s morale is 3.67
002 red wolf 3 born with strength 7,1 wolf in red headquarter
002 blue ninja 3 born with strength 4,1 ninja in blue headquarter
It has a sword and a bomb
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
It has a bomb
004 blue headquarter stops making warriors

解决问题
(1) 输出: 保留两位小数

cout << fixed<< setprecision(2)<< 数字 <<endl;

(2) 继承与多态: 将showInfo()设置为多态. 特别需要注意, 如果不是纯虚函数, 那么必须要有定义, 不然编译出错.
(3) 与原来的相比, 增加了人狗相容的例子, 将士兵和营地用指针联系在一起
(4) 用vim substitute: 将loyality替换成loyalty

:%s loyality/loyalty/g
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;class warCraft;class warrior
{protected:warCraft *mycamp;int count;int lifetime;string type;public:static string wapons[3];virtual void showInfo() = 0;warrior(warCraft * mycamp_, int count_,int lifetime_,string type_){mycamp = mycamp_;count = count_;lifetime = lifetime_;type = type_;}
};
string warrior::wapons[3] = {"sword","bomb","arrow"};class dragon:public warrior
{private:double morale;string wapon;public:dragon(warCraft *mycamp_,int count_,int lifetime_,string type_,double morale_):warrior(mycamp_,count_,lifetime_,type_){morale = morale_;wapon = wapons[count % 3];}void showInfo(){cout << fixed;cout << "It has a "<<wapon<<",and it's morale is "<<setprecision(2)<<morale<<endl;}};class ninja:public warrior
{private:string wapon[2];public:ninja(warCraft *mycamp_,int count_,int lifetime_,string type_):warrior(mycamp_,count_,lifetime_,type_){wapon[0] = wapons[count % 3];wapon[1] = wapons[(count +1) % 3];}void showInfo(){cout << "It has a "<<wapon[0]<<" and a "<<wapon[1]<<endl;}};class iceman:public warrior
{private:string wapon;public:iceman(warCraft * mycamp_,int count_,int lifetime_,string type_):warrior(mycamp_,count_,lifetime_,type_){wapon = wapons[count % 3];}void showInfo(){cout << "It has a "<<wapon<<endl;}};class lion:public warrior
{private:int loyalty;public:lion(warCraft *mycamp_,int count_,int lifetime_,string type_,int loyalty):warrior(mycamp_,count_,lifetime_,type_){this->loyalty = loyalty;}void showInfo(){cout << "It's loyalty is "<<loyalty<<endl;}
};class wolf:public warrior
{public:wolf(warCraft * mycamp_,int count_,int lifetime_,string type_):warrior(mycamp_,count_,lifetime_,type_){}void showInfo(){}};class warCraft
{friend class warrior;private:int count;              // total soilder we haveint time;               //current time       int id;                 //current created soilder, type idstring color;           //color of the warCraftstring type[5];         //order cycle of soildersint life[5];            //lifetime cycle of soildresint numberofeach[5];    // number of each soilders in the warCraftint totallife;          //total life timewarrior * mywarriors[1000];public:warCraft(int totallife_, string c, string t[], int l[]);bool generate();void printCurrent(bool);~warCraft(){for(int i = 1;i <= count;i++)delete mywarriors[i];}
};warCraft::warCraft(int totallife_, string c, string t[], int l[]):count(0),id(4),time(0)
{totallife = totallife_;color = c;for (int i = 0;i<5;i++){type[i] = t[i];life[i] = l[i];numberofeach[i] = 0;}
}bool warCraft::generate()
{time++;//next targetid = (id+1)%5;int t = 0;while(totallife - life[(id+t)%5] < 0 && t < 5){t++;}if(t == 5){printCurrent(false);return false;}else{id = (id + t)%5;totallife -= life[id];//create warrior nowwarrior *current;count++;if(type[id] == "dragon"){current = new dragon(this,count,life[id],type[id],totallife/(double)life[id]);}if(type[id] == "ninja"){current = new ninja(this,count,life[id],type[id]);}if(type[id] == "lion"){current = new lion(this,count,life[id],type[id], totallife);}if(type[id] == "iceman"){current = new iceman(this,count,life[id],type[id]);}if(type[id] == "wolf"){current = new wolf(this,count,life[id],type[id]);}numberofeach[id]++;mywarriors[count] = current;printCurrent(true);current->showInfo();return true;}}void warCraft::printCurrent(bool suss)
{cout << setw(3)<<setfill('0')<<time-1<<" ";if(suss){       cout << color <<" "<<  type[id]<<" "<<count << " born with strength "<<life[id]<<",";cout << numberofeach[id] << " " <<type[id]<< " in " << color << " headquarter"<<endl;}else{cout << color << " headquarter stops making warriors"<<endl;}
}int main()
{int n;cin >> n;string redtype[5] = {"iceman","lion","wolf","ninja","dragon"};string bluetype[5] = {"lion","dragon","ninja","iceman","wolf"};int redcycle[] = {2,3,4,1,0};int bluecycle[] = {3,0,1,2,4};string redcolor = "red";string bluecolor = "blue";int redlife[5];int bluelife[5];for(int i = 1;i<=n;i++){int M;cin >> M;int a[5];for(int j = 0;j<5;j++){cin >> a[j];}for(int k = 0;k<5;k++){redlife[k] = a[redcycle[k]];bluelife[k] = a[bluecycle[k]];}cout << "Case:" << i<<endl;warCraft red(M, redcolor,redtype,redlife);warCraft blue(M, bluecolor, bluetype,bluelife);bool r,b;int c1=0,c2=0;while(true){if(c1 == 0){r = red.generate();if(r == false)c1 = 1;}if(c2 == 0){b = blue.generate();if(b == false)c2 = 1;}if( c1 ==1 && c2 ==1)break;}}}

C++ 程序设计 week5 魔兽世界二: 装备相关推荐

  1. 20155328 《Java程序设计》 实验二(Java面向对象程序设计) 实验报告

    20155328 <Java程序设计> 实验二(Java面向对象程序设计) 实验报告 单元测试 一.单元测试和TDD 编程时需理清思路,将编程需求等想好,再开始编.此部分可用伪代码实现. ...

  2. python程序设计梁勇 百度网盘_20194220 2019-2020-2 《Python程序设计》实验二报告

    20194220 2019-2020-2 <Python程序设计>实验二报告 课程:<Python程序设计> 班级: 1942 姓名: 梁勇 学号:20194220 实验教师: ...

  3. 自考c语言程序阅读题,自考C语言程序设计模拟试题二答案.DOC

    本资料由广州自考网收集整理,更多自考资料请登录 HYPERLINK "http://www.gzzk.cc" www.gzzk.cc下载 再长的路,一步步也能走完,再短的路,不迈开 ...

  4. c语言程序设计移动字母,C语言程序设计模拟试题二(含答案)

    C语言程序设计模拟试题二(含答案) 1.下列电子邮件地址中正确的是(其中□表示空格) A) Malin&http://www.doczj.com/doc/2969157fa26925c52cc ...

  5. python实验二报告_20172304 2019-2020-2 《Python程序设计》实验二报告

    20172304 2019-2020-2 <Python程序设计>实验二报告 课程:<Python程序设计> 班级: 1723 姓名: 段志轩 学号:20172304 实验教师 ...

  6. java构成对象的三要素_20155237 《JAVA程序设计》实验二(JAVA面向对象程序设计)实验报告...

    20155237 <JAVA程序设计>实验二(JAVA面向对象程序设计)实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S ...

  7. 20145207《Java程序设计》实验二(Java面向对象程序设计)实验报告

    <Java程序设计>实验二(Java面向对象程序设计)实验报告 目录 改变 Java面向对象程序设计实验要求 实验成果 课后思考 改变 看了下之前实验二的整体,很搞笑,大图+代码,没了.. ...

  8. 《Python语言程序设计》实验二

    <Python语言程序设计>实验二 一.实验项目:Python语言序列数据结构2 1.字典运算及应用 2.集合运算及应用 3.可迭代对象应用 4.理解并运用内置函数enumerate(), ...

  9. C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序

    C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序 实验内容:Windows 窗体程序 一.实验目的及要求 二.实验环境 三.实验内容与步骤 一.设计简单的计算器 3.1.实验内容 ...

最新文章

  1. Blender三维建筑场景动画制作学习教程
  2. ORM表之间的多对多处理
  3. 中过滤记录中时间_水肥一体化中常见的过滤器
  4. python代码块使用缩进表示-python中的代码块使用缩进来表示。
  5. Docker镜像相关命令
  6. php abstract
  7. 解决win10安卓虚拟机每十几分钟蓝屏重启问题
  8. linux 系统盘无法ls,系统故障排除
  9. 18 线程池,线程安全队列,线程创建
  10. android 复制应用程序,Android限制复制,粘贴在应用程序之间查看
  11. OpenCV之分离和合并颜色通道
  12. Python技术公众号100天了
  13. 2018年华尔街高盛、花旗等投行业绩创新高!
  14. 【答题卡识别】基于matlab形态学答题卡识别【含Matlab源码 1135期】
  15. 线性代数常用的公式LaTeX表示
  16. CANoe软件安装失败
  17. Linux系统:基本开发工具
  18. oracle删除行 锁表,oracle锁表
  19. python-docx 复制一页_python 怎么用docx读取word的某一页然后放到新的word文档中?...
  20. 商业银行最全数据-66年跨度、100+指标(涵盖业务、股东、高管、员工、审计等数据)

热门文章

  1. Java中 方法引用、Stream流、及方法实例 D190401
  2. 修复版动态视频壁纸微信小程序源码下载,支持多种类型流量主收益
  3. easyui treegrid php,Easyui在treegrid添加控件实例教程
  4. 讲给后台程序员看的前端系列教程(19)——CSS的层叠性与继承性以及优先级
  5. 洛龙区:加快布局大数据产业
  6. 网易卡搭python怎么用_python爬取+使用网易卡搭作品数量api
  7. 患上糖尿病,为什么要做OCT眼底照相检查?
  8. js数组拆分成几个数组
  9. <山东大学项目实训>——Unity开发植物大战僵尸(1)
  10. 榆熙教育:电商运营必备关键指标公式