C++面向对象程序设计 第三周 类与对象(二)

一、Week3_011 Big & Base 封闭类问题

输入
多组数据,每组一行,是一个整数
输出
对每组数据,输出两行,每行把输入的整数打印两遍
样例输入
3
4
样例输出
3,3
3,3
4,4
4,4

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
        int k;
        Base(int n) :k(n) { }
};

class Big
{
public:
        int v;
        Base b;
        // 在此处补充你的代码
        Big(int b_) :v(b_),b(b_) { }
        // 在此处补充你的代码
};
int main()
{
        int n;
        while (cin >> n) {
               Big a1(n);
               Big a2 = a1;
               cout << a1.v << "," << a1.b.k << endl;
               cout << a2.v << "," << a2.b.k << endl;
        }
}

二、Week3_012 编程填空:统计动物数量

输入

输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
样例输入
None
样例输出
0 animals in the zoo, 0 of them are dogs, 0 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats
6 animals in the zoo, 3 of them are dogs, 3 of them are cats
3 animals in the zoo, 2 of them are dogs, 1 of them are cats

#include "stdafx.h"

#include <iostream>
using namespace std;
// 在此处补充你的代码
class Animal
{
public:
        static int number;
        Animal();
        virtual ~Animal();// 这里不加virtual就会有误,为何呢?
        Animal(const Animal & a);

private:
};
int Animal::number = 0;
Animal::Animal(){number++;}
Animal::~Animal(){number--;}
Animal::Animal(const Animal &a) {     number++;}
class Dog:public Animal
{
public:
        static int number;
        Dog();
        ~Dog();
};
int Dog::number = 0;
Dog::Dog(){    number++;}
Dog::~Dog(){ number--;}
class Cat:public Animal
{
public:
        static int number;
        Cat();
        ~Cat();
        Cat(const Cat& c);
};
int Cat::number = 0;
Cat::Cat(){    number++;}
Cat::~Cat(){ -- number;}
Cat::Cat(const Cat& c) { number++; }
// 在此处补充你的代码
void print() {
        cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}

int main() {
        print();
        Dog d1, d2;
        Cat c1;
        print();
        Dog* d3 = new Dog();
        Animal* c2 = new Cat;
        Cat* c3 = new Cat;
        print();
        delete c3;
        delete c2;
        delete d3;
        print();
}

三、Week3_013 这个指针哪来的

输入

输出
10
样例输入

样例输出
10

#include "stdafx.h"
#include <iostream>
using namespace std;
struct A
{
        int v;
        A(int vv) :v(vv) { }
        // 在此处补充你的代码
        const A * getPointer() const {
               return this;
        }
        // 在此处补充你的代码
};
int main()
{
        const A a(10);
        const A * p = a.getPointer();
        cout << p->v << endl;
        return 0;
}

四、Week3_014 魔兽世界之一:备战

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

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

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

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

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

红方司令部按照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武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。

  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
000 blue lion 1 born with strength 6,1 lion in blue headquarter
001 red lion 2 born with strength 6,1 lion in red headquarter
001 blue dragon 2 born with strength 3,1 dragon in blue headquarter
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
003 red headquarter stops making warriors
003 blue iceman 4 born with strength 5,1 iceman in blue headquarter
004 blue headquarter stops making warriors

#include<cstring>
#include <iostream>
#include <stdio.h>
using namespace std;
#define WARRIOR_NUM 5
/*
char * CWarrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
红方司令部按照 iceman、lion、wolf、ninja、dragon 的顺序制造武士。
蓝方司令部按照 lion、dragon、ninja、iceman、wolf 的顺序制造武士。
*/
class CHeadquarter;
class CWarrior
{
private:
        CHeadquarter * pHeadquarter;
        int kindNo; //武士的种类编号 0 dragon 1 ninja 2 iceman 3 lion 4 wolf
        int nNo;       //该阵营的第几个战士
public:
        static char * names[WARRIOR_NUM];              //输入生命值的顺序 d、n、i、l、w
        static int InitialLifeValue[WARRIOR_NUM]; // 每个武士的初始生命值
        CWarrior(CHeadquarter * p, int nNo_, int kindNo_);
        void PrintResult(int nTime);              //打印生成战士的信息
};
class CHeadquarter  //大本营
{
private:
        int totalLifeValue;
        bool bStopped;       //判断是否停止制造武士
        int totalWarriorNum; //总武士数量
        int color;                     //;不同大本营的颜色
        int curMakingSeqIdx; //当前要制造的武士是制造序列中的第几个
        int warriorNum[WARRIOR_NUM]; //存放每种武士的数量
        CWarrior * pWarriors[1000];
public:
        friend class CWarrior;
        static int makingSeq[2][WARRIOR_NUM]; //武士的制作顺序序列,全局变量初始化
        void Init(int color_, int lv);        //初始化某大本营的总生命值、停止标志设为false、从一个序号开始制作士兵、将该大本营的全部种类士兵设为0。
        ~CHeadquarter();                                       //当大本营解散时,释放所有士兵所占用的空间。
        int Produce(int nTime);                                //生成武士
        void GetColor(char * szColor);        //初始化该大本营的颜色
};

void CWarrior::PrintResult(int nTime)
{
        char szColor[20];
        pHeadquarter->GetColor(szColor);
        printf("%03d %s %s %d born with strength %d,%d %s in %s headquarter\n", //03d:用0补齐;
               nTime, szColor, names[kindNo], nNo, InitialLifeValue[kindNo],
               pHeadquarter->warriorNum[kindNo], names[kindNo], szColor);
}


int CWarrior::InitialLifeValue[WARRIOR_NUM];
char * CWarrior::names[WARRIOR_NUM] = { "dragon","ninja","iceman","lion","wolf" };
CWarrior::CWarrior(CHeadquarter * p, int nNo_, int kindNo_) :nNo(nNo_), kindNo(kindNo_), pHeadquarter(p) { }
int CHeadquarter::makingSeq[2][WARRIOR_NUM] = { { 2,3,4,1,0 },{ 3,0,1,2,4 } }; //两个司令部武士的制作顺序序列
void CHeadquarter::Init(int color_, int lv) //初始化某大本营的总生命值、停止标志设为false、从一个序号开始制作士兵、将该大本营的全部种类士兵设为0。
{
        color = color_;
        totalLifeValue = lv;
        totalWarriorNum = 0;
        bStopped = false;
        curMakingSeqIdx = 0;
        for (int i = 0; i < WARRIOR_NUM; i++)
               warriorNum[i] = 0;
}
CHeadquarter::~CHeadquarter() //当大本营解散时,释放所有士兵所占用的空间。
{       
        for (int i = 0; i < totalWarriorNum; i++) 
               delete pWarriors[i];
}  
void CHeadquarter::GetColor(char * szColor)
{
        if (color == 0)
               strcpy(szColor, "red");
        else
               strcpy(szColor, "blue");
}
int CHeadquarter::Produce(int nTime)
{
        if (bStopped)
               return 0;
        int nSearchingTimes = 0;             //该生成的武士序列
        while (CWarrior::InitialLifeValue[makingSeq[color][curMakingSeqIdx]] > totalLifeValue &&
               nSearchingTimes < WARRIOR_NUM) {
               curMakingSeqIdx = (curMakingSeqIdx + 1) % WARRIOR_NUM; //使curMakingSeqIdx不超出序列范围
               nSearchingTimes++;
        }

        int kindNo = makingSeq[color][curMakingSeqIdx];
        if (CWarrior::InitialLifeValue[kindNo] > totalLifeValue) { // 剩余生命值不足以制作武士
               bStopped = true;
               if (color == 0)
                       printf("%03d red headquarter stops making warriors\n", nTime);
               else
                       printf("%03d blue headquarter stops making warriors\n", nTime);
               return 0;
        }
        totalLifeValue -= CWarrior::InitialLifeValue[kindNo];
        curMakingSeqIdx = (curMakingSeqIdx + 1) % WARRIOR_NUM;
        pWarriors[totalWarriorNum] = new CWarrior(this, totalWarriorNum + 1, kindNo);
        warriorNum[kindNo]++;
        pWarriors[totalWarriorNum]->PrintResult(nTime);
        totalWarriorNum++;
        return 1;
}



int main()
{
        int t;
        int m;
        CHeadquarter RedHead, BlueHead;
        scanf("%d", &t);
        int nCaseNo = 1;
        while (t--) {
               printf("Case:%d\n", nCaseNo++);
               scanf("%d", &m);
               for (int i = 0; i < WARRIOR_NUM; i++)
                       scanf("%d", &CWarrior::InitialLifeValue[i]);
               RedHead.Init(0, m);
               BlueHead.Init(1, m);
               int nTime = 0;      //事件的开始时间
               while (true) {
                       int tmp1 = RedHead.Produce(nTime); //某一大本营停止制造武士返回值就设为0
                       int tmp2 = BlueHead.Produce(nTime);
                       if (tmp1 == 0 && tmp2 == 0)
                              break;
                       nTime++;
               }
        }
        return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值