【Cousera作业】魔兽世界之一:备战


样例输入
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


这道题踩了无数的坑,错误的原因有如下:

1. 自己写的算法太乐色,之前调用了太多的次的构造函数和复制构造函数,然后time limit exceeded了,现在用了指针数组来指向士兵对象,这样就不用再调用构造和复制构造函数了。然后每个case中共有的东西(换句话说就是不用输入的数据)可以作为公共数值在循环外先赋值。

2. hp和strength是相等的,这个题目里没写……还以为是固定的

3. “如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士。” 这个逻辑有很多细节要注意,我自己也实现的非常啰嗦,代码可读性非常差。

4. 这道题是没问题的,如果是出现了wrong answer, 一定是你自己的代码出了问题, 不要有自己的蜜汁自信

使用了派生类,是因为考虑到后面更为复杂的两题,所以为了增加一些代码的可扩充性。等有空了再继续改进代码……

#include 
#include 
#include 

using namespace std;

const int Num_of_Type = 5;

//战士区
class warrior
{
protected:
    char warrior_name[10];
    int strength;
public:
    int type_id;
    int hp;
    void GetHp(int hp1)
    {
        hp = hp1;
        strength = hp1; //strength和hp相同,大坑
    }

friend class headquarter;
};

class dragon:public warrior
{
private:

public:
    dragon()
    {
        strcpy(warrior_name, "dragon");
        strength = 0;
        hp = 0;
        type_id = 1;
    }
};

class ninja:public warrior
{
private:

public:
    ninja()
    {
        strcpy(warrior_name, "ninja");
        strength = 0;
        this->hp = hp;
        type_id = 2;
    }
};

class iceman:public warrior
{
private:

public:
    iceman()
    {
        strcpy(warrior_name, "iceman");
        strength = 0;
        hp = 0;
        type_id = 3;
    }
};

class wolf:public warrior
{
public:
    wolf()
    {
        strcpy(warrior_name, "wolf");
        strength = 0;
        hp = 0;
        type_id = 4;
    }
};

class lion:public warrior
{
private:

public:
    lion()
    {
        strcpy(warrior_name, "lion");
        strength = 0;
        hp = 0;
        type_id = 5;
    }
};

//基地区
class headquarter
{
private:
    int resource; //基地生命值
    char name[10];
    int num_of_warrior;
    int round;
    int waste; //记录
    int num_of_each_warrior[Num_of_Type];
public:
    warrior* order[Num_of_Type];
    bool declared;
    //构造函数
    headquarter(const char a1[])
    {
        strcpy(name, a1);
        resource = 0;
        num_of_warrior = 0;
        waste = 0;
        declared = false;
        for(int i = 0; i < Num_of_Type; i++)
        {
            num_of_each_warrior[i]= 0;
        }
    }
    //获得资源值函数
    void GetResourceAndReset(int m)
    {
        resource = m;
        declared = false;
        num_of_warrior = 0;
        waste = 0;
        for(int i = 0; i < Num_of_Type; i++)
        {
            num_of_each_warrior[i] = 0;
        }
    }
    //造兵函数
    void Produce(const int time)
    {
        round = (time+waste) % Num_of_Type;
        for(int j = round; j < round + Num_of_Type; j++)
        {
            int real_round = j;
            if(real_round >= Num_of_Type)
            {
                real_round -= Num_of_Type;
            }
            //找到了可以制造的士兵则结束循环
            if(resource-order[real_round]->hp>=0){
                waste += (j - round); //记录下循环了几次才找到要找的士兵,下次进入produce函数时需要加上这个数值
                num_of_each_warrior[real_round]++;
                num_of_warrior++;
                resource -= order[real_round]->hp;
                cout<< setw(3) << setfill('0') << time
                << " " << name <<" "<warrior_name<<" "
                << num_of_warrior <<" " << "born with strength "<strength
                <<","<warrior_name <<" "<< "in" <<" "<> test_set;
    int resource[test_set];
    int hp[test_set][Num_of_Type];
    for(int i = 0; i < test_set; i++)
    {
        cin >> resource[i];
        for(int j = 0; j < Num_of_Type; j++)
        {
            cin >> hp[i][j];
        }
    }

    int time = 0;
    //初始化战士对象
    dragon dragon1;
    iceman iceman1;
    ninja ninja1;
    wolf wolf1;
    lion lion1;
    //初始化基地
    headquarter red("red");
    headquarter blue("blue");
    //红色基地指针数组
    red.order[0]=&iceman1;
    red.order[1]=&lion1;
    red.order[2]=&wolf1;
    red.order[3]=&ninja1;
    red.order[4]=&dragon1;

    blue.order[0]=&lion1;
    blue.order[1]=&dragon1;
    blue.order[2]=&ninja1;
    blue.order[3]=&iceman1;
    blue.order[4]=&wolf1;
    //case循环
    for(int i = 0; i < test_set; i++)
    {
        cout << "Case:" << i+1 << endl;
        //找到输入的hp的最小值,用来判断资源是否足够制造任何一个士兵
        int min_requirement = 1000;
        for(int j = 0; j < Num_of_Type; j++)
        {
            if(hp[i][j]



你可能感兴趣的:(C++)