用队列解决这个问题,三个队列分别代表着三个人的手牌数。假如Alice出牌一次,则她的队列中出掉第一张牌,在进行这操作时先判断其队列是否为空,若队列为空则其胜利。其余两人依次类推。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String Sa = sc.next();
String Sb = sc.next();
String Sc = sc.next();
queue lista = new queue();
queue listb = new queue();
queue listc = new queue();
for (int i = 0; i < Sa.length(); i++) {
Node newNode = new Node(Sa.charAt(i));
lista.add(newNode);
}
for (int i = 0; i < Sb.length(); i++) {
Node newNode = new Node(Sb.charAt(i));
listb.add(newNode);
}
for (int i = 0; i < Sc.length(); i++) {
Node newNode = new Node(Sc.charAt(i));
listc.add(newNode);
}
char jk = lista.front();
lista.remove();
while(true){
if(jk == 'a'){
if(lista.isEmpty()){
System.out.println("A");
break;
}else{
jk = lista.front();
lista.remove();
}
}else if(jk == 'b'){
if(listb.isEmpty()){
System.out.println("B");
break;
}else {
jk = listb.front();
listb.remove();
}
}else {
if(listc.isEmpty()){
System.out.println("C");
break;
}else {
jk = listc.front();
listc.remove();
}
}
}
}
}
}
//队列类
class queue{
//定义头结点
Node head = new Node(' ');
//添加操作
public void add(Node newNode){
//工作指针
Node temp = head;
//遍历到链表尾部
while(true){
if(temp.next == null) break;
temp = temp.next;
}
temp.next = newNode;
}
//判空
public boolean isEmpty(){
if( head.next == null) return true;
else return false;
}
//出队列
public void remove (){
head.next = head.next.next;
}
//返回队列头元素
public char front(){
return head.next.str;
}
//遍历输出
public void display (){
Node temp = head;
while(true){
if(temp.next == null) break;
System.out.print(temp.next.str);
temp = temp.next;
}
System.out.println();
}
}
//节点类
class Node{
public char str;
Node next;
public Node(char str){
this.str = str;
}
@Override
public String toString() {
return "Node{" +
"str='" + str + '\'' +
'}';
}
}