Alice,Bob and Charlie are playing Card

在这里插入图片描述
用队列解决这个问题,三个队列分别代表着三个人的手牌数。假如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 + '\'' +
                '}';
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在地球迷路的怪兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值