013:魔兽世界之一:备战

一开始想看别人答案写,但都好长,注释也不够多,放弃了。然后自己一点点想,一点点测试,写了半天时间,调试了超级久,终于过了(大哭),发个文章纪念一下

两个类的定义:(感觉这一题还用不到Warrior类)

#include<iostream>
#include<cstring>
using namespace std;

// 记dragon=0,ninja=1,iceman=2,lion=3,wolf=4
const string Name[5] = {"dragon", "ninja", "iceman", "lion", "wolf"};
const int ROrder[5] = {2, 3, 4, 1, 0};
const int BOrder[5] = {3, 0, 1, 2, 4};

class Warrior
{
    private:
        int no;         // 编号
        int strength;   // 生命值
        int attack;     // 攻击力
        int kind;       // 种类
};

class Headquarters
{
    private:
        string Color;      // 司令部的派系
        int sValue;     // 生命元
        int Order[5];    // 生产顺序
        int WarriorNumber;  // 武士编号
        int strengthKind[5];// 用于储存各种类的武士的血量
        int index;          // 当前生产种类
        int sum[5] = {0,};         // 储存种类数量
        int loop=0;               // 循环标志,当loop=5,意味着轮过一圈都不能生产,因此该司令部不再能生产
    
    public:
        Headquarters(string color, int m, const int order[], int sK[]);
        // 构造函数,color是派系,m是生命元,order是派系生成武士顺序,sK是各种武士的血量
        void Making();      // 生成士兵,也会输出停止士兵
        bool StopSign();    // 用loop判断生产停止了吗,不输出,主要提供bool返回值
        void Reset(int M, int sK[5]);   // 每组测试需要重新对基础值赋值
};

Headquarters类的函数定义:

Headquarters::Headquarters(string color, int m, const int order[], int sK[])
        {
            Color = color;
            index = 0;
            WarriorNumber = 1;
            sValue = m;
            for (int i = 0; i < 5; i++)
            {
                Order[i] = order[i];
            }
            for (int i = 0; i < 5; i++)
            {
                strengthKind[i] = sK[i];
            }
            
        }
void Headquarters::Making()
{
    loop = 0;
    while( loop < 5 )
    {
        if( sValue >= strengthKind[Order[index]])
        {
            sValue -= strengthKind[Order[index]];
            ++sum[Order[index]];
            cout<<Color<<" "<<Name[Order[index]]<<" "<<WarriorNumber<<" born with strength "
                <<strengthKind[Order[index]]<<","<<sum[Order[index]]<<" "
                <<Name[Order[index]]<<" in "<<Color<<" headquarter"<<endl;
            index = (index<4)? index+1 : 0;
            ++WarriorNumber;
            break;
        }else    //表明当前武士不够生命元生产,index加1生产下一个
        {
            ++loop;
            index = (index<4)? index+1 : 0;   
            continue;
        }
    }
    if(loop == 5)    //loop=5,表明已经五个都试过,都生产不了,要停了
    {
        cout<<Color<<" headquarter stops making warriors"<<endl;
    }
}
bool Headquarters::StopSign()    //loop=5,表明已经五个都试过,都生产不了,真true要停了
{
    return (loop<5)? false : true;
}
void Headquarters::Reset(int M, int sK[5])    //生命元、武士编号、生产编号、循环标志、武士数量都应该回归初值,而血量重新读入
{
    sValue = M;
    WarriorNumber = 1;
    index = 0;
    loop = 0;
    for(int i=0; i<5; i++)
    {
        sum[i] = 0;
        strengthKind[i] = sK[i];
    }
}

主函数:

int main()
{
    int time = 0;
    int CaseNum;
    cin >> CaseNum;
    int M;
    cin >> M;
    int sKind[5];
    cin >> sKind[0] >> sKind[1] >> sKind[2] >> sKind[3] >> sKind[4];
    Headquarters RED("red", M, ROrder, sKind);
    Headquarters BLUE("blue", M, BOrder, sKind);

    for(int i=1; i<=CaseNum; i++){
        cout<<"Case:"<<i<<endl;
        while (1)
    {
        if(!RED.StopSign() && !BLUE.StopSign())    //如果红色司令部没有停止,但蓝色没有停止
        {
            if(time<10) cout<<"00";
            else if(time<100) cout<<"0";
            cout<<time<<" ";
            RED.Making();

            if(time<10) cout<<"00";
            else if(time<100) cout<<"0";
            cout<<time<<" ";
            BLUE.Making();
        }
        else if(RED.StopSign() && !BLUE.StopSign())    //如果红色司令部已经停止,但蓝色没有停止
        { 
            if(time<10) cout<<"00";
            else if(time<100) cout<<"0";
            cout<<time<<" ";
            BLUE.Making();
        }
        else if(!RED.StopSign() && BLUE.StopSign())     //如果红色司令部没有停止,但蓝色已经停止
        { 
            if(time<10) cout<<"00";
            else if(time<100) cout<<"0";
            cout<<time<<" ";
            RED.Making();
        }else    break;
        ++time;
    }
        cin>> M;    //读入下一组数据
        cin >> sKind[0] >> sKind[1] >> sKind[2] >> sKind[3] >> sKind[4];
        RED.Reset(M, sKind);
        BLUE.Reset(M, sKind);
        time=0;
    }

    return 0;
}

主要是测试用例太多了,有点小错真的得找半天。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值