当前位置: 代码迷 >> 综合 >> Alice,Bob and Charlie are playing Card
  详细解决方案

Alice,Bob and Charlie are playing Card

热度:14   发布时间:2023-11-27 04:07:19.0

在这里插入图片描述
用队列解决这个问题,三个队列分别代表着三个人的手牌数。假如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;}@Overridepublic String toString() {
    return "Node{" +"str='" + str + '\'' +'}';}
}
  相关解决方案