Description
魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部。两个司令部之间是依次排列的若干城市。
红司令部,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分开始到双方司令部停止制造武士为止的所有事件按顺序输出。
一共有两种事件,其对应的输出样例如下:
- 武士降生
输出样例: 004 blue lion 5 born with strength 5,2 lion in red headquarter
表示在4点整,编号为5的蓝魔lion武士降生,它降生时生命值为5,降生后蓝魔司令部里共有2个lion武士。(为简单起见,不考虑单词的复数形式)注意,每制造出一个新的武士,都要输出此时司令部里共有多少个该种武士。
- 司令部停止制造武士
输出样例: 010 red headquarter stops making warriors
表示在10点整,红方司令部停止制造武士
输出事件时:
首先按时间顺序输出;
同一时间发生的事件,先输出红司令部的,再输出蓝司令部的。
Input
第一行是一个整数,代表测试数据组数。
每组测试数据共两行。
第一行:一个整数M。其含义为, 每个司令部一开始都有M个生命元( 1 ≤ M ≤ 10000)。
第二行:五个整数,依次是 dragon 、ninja、iceman、lion、wolf 的初始生命值。它们都大于0小于等于10000。
Output
对每组测试数据,要求输出从0时0分开始,到双方司令部都停止制造武士为止的所有事件。
对每组测试数据,首先输出"Case:n" n是测试数据的编号,从1开始 ,每个Case后面输出一个空行。
接下来按恰当的顺序和格式输出所有事件。每个事件都以事件发生的时间开头,时间以小时为单位,有三位。
Sample Input
1
20
3 4 5 6 7
Sample Output
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
Code
/*离亭晚-2021-02-27 00:21:03*/
import java.util.LinkedHashMap;
import java.util.Scanner;
abstract class Command{
LinkedHashMap<Integer,String> history = new LinkedHashMap<>();//备忘武士制造<时间,具体事件>历史
String cityName;
int countIceman = 0,countLion = 0,countWolf = 0,countNinja = 0,countDragon = 0;
int cityPower,icemanPower,lionPower,wolfPower,ninjaPower,dragonPower;
boolean nextCreate = true;
int lowestPower;
int number = 0, time = 0;
public Command(int cityPower,int dragonPower,int ninjaPower,int icemanPower,int lionPower,int wolfPower,String cityName){
this.cityPower = cityPower;
this.dragonPower = dragonPower;
this.ninjaPower = ninjaPower;
this.icemanPower = icemanPower;
this.lionPower = lionPower;
this.wolfPower = wolfPower;
this.cityName = cityName;
lowestPower = getLowestPower();
createCycle();
}
/*
如果司令部中的生命元不足以制造某个按顺序应该制造的武士,那么司令部就试图制造下一个。如果所有武士都不能制造了,则司令部停止制造武士
--Command总生命元不足制造当前武士时,仍需要继续寻找...(而非即刻跳出循环)。因为可能存在下一个的下一个武士满足制造条件
因此,需要先找出最低生命元的武士,getLowestPower()结合createCycle()、judgeNextCreate以实现题目要求,
其中,createCycle在父类中是抽象方法,具体方法在子类中实现
*/
public abstract void createCycle();
public int getLowestPower(){
int tmp = 100000;
if(tmp>dragonPower) tmp = dragonPower;
if(tmp>ninjaPower) tmp = ninjaPower;
if(tmp>icemanPower) tmp = icemanPower;
if(tmp>lionPower) tmp = lionPower;
if(tmp>wolfPower) tmp = wolfPower;
return tmp;
}
public int getTime(){
return time++;
}
public void judgeNextCreate(){
if(cityPower>=lowestPower) nextCreate = true;
else nextCreate = false;
}
public void createIceman(){
if(cityPower>=icemanPower){
countIceman++;
number++;
printfSuccess("iceman",countIceman,icemanPower);
cityPower -= icemanPower;
judgeNextCreate();
}
}
public void createLion(){
if(cityPower>=lionPower){
countLion++;
number++;
printfSuccess("lion",countLion,lionPower);
cityPower -= lionPower;
judgeNextCreate();
}
}
public void createWolf(){
if(cityPower>=wolfPower){
countWolf++;
number++;
printfSuccess("wolf",countWolf,wolfPower);
cityPower -= wolfPower;
judgeNextCreate();
}
}
public void createNinja(){
if(cityPower>=ninjaPower){
countNinja++;
number++;
printfSuccess("ninja",countNinja,ninjaPower);
cityPower -= ninjaPower;
judgeNextCreate();
}
}
public void createDragon(){
if(cityPower>=dragonPower){
countDragon++;
number++;
printfSuccess("dragon",countDragon,dragonPower);
cityPower -= dragonPower;
judgeNextCreate();
}
}
public void printfSuccess(String soldier,int countSoldier,int soldierPower){
String s = cityName+" "+soldier+" "+number+" born with strength "+soldierPower+","+countSoldier+" "+soldier+" in "+cityName+" headquarter" ;
history.put(getTime(),s);
}
public void printfFails(){
String s = cityName+" headquarter stops making warriors";
history.put(getTime(),s);
}
}
/*
class BlueCommand与class RedCommand子类继承Command父类,但需重写createCycle()方法,并在其中按不同次序调用各Create方法,以实现:
“红方司令部按照iceman、lion、wolf、ninja、dragon的顺序循环制造武士。
蓝方司令部按照lion、dragon、ninja、iceman、wolf的顺序循环制造武士。”
*/
class RedCommand extends Command{
public RedCommand(int cityPower, int dragonPower, int ninjaPower, int icemanPower, int lionPower, int wolfPower, String cityName) {
super(cityPower, dragonPower, ninjaPower, icemanPower, lionPower, wolfPower, cityName);
}
@Override
public void createCycle(){
while(nextCreate){//next,可以是当前下一个或下一个的下一个...
createIceman();
createLion();
createWolf();
createNinja();
createDragon();
}
printfFails();
}
}
class BlueCommand extends Command{
public BlueCommand(int cityPower, int dragonPower, int ninjaPower, int icemanPower, int lionPower, int wolfPower, String cityName) {
super(cityPower, dragonPower, ninjaPower, icemanPower, lionPower, wolfPower, cityName);
}
@Override
public void createCycle(){
while(nextCreate){
createLion();
createDragon();
createNinja();
createIceman();
createWolf();
}
printfFails();
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 1; i <= T; i++) {
int cityPower = sc.nextInt();
int dragonPower = sc.nextInt();
int ninjaPower = sc.nextInt();
int icemanPower = sc.nextInt();
int lionPower = sc.nextInt();
int wolfPower = sc.nextInt();
RedCommand red = new RedCommand(cityPower,dragonPower,ninjaPower,icemanPower,lionPower,wolfPower,"red");
BlueCommand blue = new BlueCommand(cityPower,dragonPower,ninjaPower,icemanPower,lionPower,wolfPower,"blue");
int redHistorySize = red.history.size()-1;
int blueHistorySize = blue.history.size()-1;
int times = 0;
System.out.println("Case:"+i);
while(redHistorySize>=0||blueHistorySize>=0){
if(red.history.containsKey(times)){
String time = String.format("%3d",times).replace(" ","0");
System.out.println(time +" " +red.history.get(times));
}
if(blue.history.containsKey(times)){
String time = String.format("%3d",times).replace(" ","0");
System.out.println(time +" " +blue.history.get(times));
}
times++;
redHistorySize--;
blueHistorySize--;
}
}
}
}