欢迎访问我的CCF认证解题目录
题目描述
思路过程
召唤角色时,当前位置和右边位置的角色编号会+1,而当角色死亡时,右边位置的角色编号会-1,这里可以使用java内置的List来完成这个操作,用下标来表示编号,直接插入和删除即可完成上面两个操作。
用flag
来标记当前操作的玩家
注意:英雄死亡不清空
代码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();//指令数量
List<User> user1 = new LinkedList<>(), user2 = new LinkedList<>();//模拟两方的角色
//初始化英雄
user1.add(new User(0, 30));
user2.add(new User(0, 30));
boolean flag = true;//标记现在操作的玩家
in.nextLine();//指令数量后面的换行
while ( n-- > 0 ) {
String[] order = in.nextLine().split(" ");//按空格分割指令
if ( order[0].equals("summon") ) {
int num = Integer.parseInt(order[1]), attack = Integer.parseInt(order[2]), health = Integer.parseInt(order[3]);
if ( flag ) user1.add(num, new User(attack, health));//如果是先手玩家
else user2.add(num, new User(attack, health));
} else if ( order[0].equals("attack") ) {
//num1表示先手玩家的角色,num2表示另一玩家的角色
int num1 = Integer.parseInt(order[1]), num2 = Integer.parseInt(order[2]);
if ( !flag ) {//如果现在的回合不是先手玩家,则交换一下值
int temp = num1;
num1 = num2;
num2 = temp;
}
user1.get(num1).health -= user2.get(num2).attack;
user2.get(num2).health -= user1.get(num1).attack;
//英雄角色死亡不消除
if ( user1.get(num1).health <= 0 && num1 != 0 ) user1.remove(num1);
if ( user2.get(num2).health <= 0 && num2 != 0 ) user2.remove(num2);
} else {
flag = !flag;
}
}
//输出第一行
if ( user1.get(0).health > 0 && user2.get(0).health > 0 ) {
System.out.println(0);
} else if ( user2.get(0).health <= 0 ) {
System.out.println(1);
} else System.out.println(-1);
//按要求输出对应的信息
print(user1);
print(user2);
}
//输出2、3行
public static void print( List<User> user ) {
System.out.println(user.get(0).health);
System.out.print(user.size()-1);
for ( int i = 1; i < user.size(); i++ ) System.out.print(" "+user.get(i).health);
System.out.println();
}
}
class User {
int attack;//攻击
int health;//生命
public User(int attack, int health) {
this.attack = attack;
this.health = health;
}
}