B:魔兽世界之一:备战

http://cxsjsxmooc.openjudge.cn/test/B/

系统关闭,未测试

#include <iostream>
#include <iomanip>
using namespace std;
class headquarters;
extern int Time;
class warrior
{
private:
    int num;
    const int life;

public:
    friend class headquarters;
    const int id;
    const static char name[][10];
    static int lifes[];
    warrior *next;
    warrior(int _id, int _life, int _num = 0) : id(_id), num(_num), life(_life), next(NULL){};
    bool create(int &w)
    {
        if (w >= life)
        {
            w -= life;
            num++;
            return true;
        }
        else
            return false;
    }
};
class headquarters
{
private:
    int life;
    warrior *pp;
    const int *order;
    const char *color;
    bool stop;
    int num;

public:
    headquarters(int _life, const int _order[], const char *_color) : pp(NULL), life(_life), order(_order), color(_color), stop(false), num(0)
    {
    }
    ~headquarters()
    {
        while (pp != NULL && pp->next != pp)
        {
            warrior *temp;
            temp = pp->next->next;
            delete pp->next;
            pp->next = temp;
        }
        delete pp;
    }
    void add_node(int _id)
    {
        warrior *temp = new warrior(_id, warrior::lifes[_id]);
        if (pp == NULL)
        {
            pp = temp;
            pp->next = pp;
        }
        else
        {
            temp->next = pp->next;
            pp->next = temp;
            pp = temp;
        }
    }
    bool making()
    {
        if (stop)
            return false;
        pp = pp->next;
        if (pp->create(life))
        {
            num++;
            cout << setw(3) << setfill('0') << Time << ' ';
            cout << color << ' ' << warrior::name[pp->id] << ' ' <<num <<' '<<"born with strength "<<pp->life<<","<< pp->num <<" "<<warrior::name[pp->id]<<" in "<<color<<" headquarter"<< endl;
            return true;
        }
        else
        {
            warrior *temp = pp->next;
            while (temp != pp)
            {

                if (temp->create(life))
                {
                    num++;
                    cout << setw(3) << setfill('0') << Time << ' ';
                    pp = temp;
                    cout << color << ' ' << warrior::name[temp->id] << ' ' <<num<<' '<<"born with strength "<<temp->life<<"," << temp->num <<" "<<warrior::name[temp->id]<<" in "<<color<<" headquarter"<<endl;
                    return true;
                }
                temp = temp->next;
            }
            stop = true;
            cout << setw(3) << setfill('0') << Time << ' ';
            cout << color << " headquarter stops making warriors" << endl;
            return false;
        }
    }
};
const char warrior::name[][10]{"dragon", "ninja", "iceman", "lion", "wolf"};
int warrior::lifes[5]{0};
const int _order[2][5]{{2, 3, 4, 1, 0}, {3, 0, 1, 2, 4}};
const char color[][5] = {"red", "blue"};

int Time = 0;
int main()
{
    int n;
    cin >> n;
    for (int j = 1; j <= n; j++)
    {
        cout << "Case:" << j << endl;
        int h_life;
        cin >> h_life;
        for (int i = 0; i < 5; i++)
        {
            cin >> warrior::lifes[i];
        }
        headquarters red(h_life, _order[0], color[0]);
        headquarters blue(h_life, _order[1], color[1]);
        for (int k = 0; k < 5; k++)
        {
            red.add_node(_order[0][k]);
            blue.add_node(_order[1][k]);
        }
        for (Time = 0;; Time++)
        {

            bool a, b;

            a = red.making();

            b = blue.making();
            if (!(a || b))
                break;
        }
    }
    return 0;
}

发布日期:
分类:C++

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注