魔兽世界之一:备战

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。 
红司令部,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

这道题就是MOOC上北大C++第三周作业的最后一题

一开始一直通不过额,因为Xcode的输入环境有限制行数,然后用bash看了下,发现是把ninja 打成了ninjia -  - 一口老血喷出来

重点是这一句啦: g++ 开头,- o表示输出文件,输出文件的名字叫main, 那么main就是一个可执行文件

 g++ -o main main.cpp

然后对main执行, < 表示输入的是哪个文件,我把in-2这个文件放在Week文件下了,所以 ./表示本文件夹目录下即可。

 ./main < ./in-2.txt

然后对于代码,唯一的一个trick就是,我把count设为了初始值-1;

//魔兽世界开始备战- -
# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string>
void Ready();
using namespace std;

/* warrior class is unnecessary now,
 but i think in the future it will be useful when we consider the live of every warrior*/
class warrior{
private:
    int live;
public:
};

class Team{
private:
    int label;              /* red or blue */
    int time;               /* time & how many warriors in total */
    int count;              /* count is the signal of which warrior is going to be born */
    int exist[5];           /* intialize the warrior number to 0 */
    int liveOfTeam[5];      /* intialize the blood of warrior */
    int totalBlood;         /* total blood of a team */
    string order[5] = {};   /* order of warrior to be produced */
public:
    int endLabel;           /* set it to public in order to control the while loop */
    Team( const string orderIn[], int total, int live[], int l);
    void print();
    void born();
    void endprint();
};

/* Initialize the team */
Team::Team(const string orderIn[], int total, int live[], int l){
    for(int i = 0; i < 5; i++){
        order[i] = orderIn[i];
        exist[i] = 0;
        liveOfTeam[i] = live[i];
    }
    totalBlood = total;
    time = 0;
    count = -1;
    label = l;
    endLabel = 0;
}

/* born a new warrior */
void Team::born(){
    int i,j;
    int tmp = count;
    if(tmp+1 == 5)
        tmp = -1;
    if(totalBlood >= 0 && endLabel == 0){
        for (i = tmp+1; i < 5; ++i) {
            if(totalBlood - liveOfTeam[i] >= 0){
                totalBlood -= liveOfTeam[i];
                ++exist[i];
                count = i;
                print();
                ++time;
                return;
            }
        }
        for(j = 0; j <= tmp; ++j){
            if(totalBlood - liveOfTeam[j] >= 0){
                totalBlood -= liveOfTeam[j];
                ++exist[j];
                count = j;
                print();
                ++time;
                return;
            }
        }
        
        /* if none of above is satisfied, than set the endlabel to 1 and ndprint */
        endLabel = 1;
        endprint();
    }
}

/* print one line everytime a warrior is born */
void Team::print(){
    if(label == 0)
        printf("%03d red %s %d born with strength %d,%d %s in red headquarter\n",time,order[count].c_str(),time+1,liveOfTeam[count],exist[count],order[count].c_str());
    if(label == 1)
        printf("%03d blue %s %d born with strength %d,%d %s in blue headquarter\n",time,order[count].c_str(),time+1,liveOfTeam[count],exist[count],order[count].c_str());
        
}

/* print one line when no worrior can be born any more */
void Team::endprint(){
    if(label == 0)
        printf("%03d red headquarter stops making warriors\n",time);
    if(label == 1)
        printf("%03d blue headquarter stops making warriors\n",time);
}

/* the ready function */
void Ready(){
    int total;
    int iceman,lion,wolf,ninjia,dragon;
    
    cin >> total;
    cin >> dragon >> ninjia >> iceman >> lion >> wolf;
    
    string redOrder[5] = {"iceman","lion","wolf","ninja","dragon"};
    string blueOrder[5] = {"lion","dragon","ninja","iceman","wolf"};
    
    int liveOfRed[5] = {iceman,lion,wolf,ninjia,dragon};
    int liveOfBlue[5] = {lion,dragon,ninjia,iceman,wolf};
    
    Team red(redOrder,total,liveOfRed,0);
    Team blue(blueOrder,total,liveOfBlue,1);
    
    while(red.endLabel == 0 || blue.endLabel == 0){
        red.born();
        blue.born();
    }
}

int main(){
    int number;
    cin >> number;
    int i = 0;
    
    while(number --){
        cout << "Case:" << ++i << endl;
        Ready();
    };
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_25175067/article/details/79894753