蓝桥杯第八届真题 :拉马车

点击查看:蓝桥杯历年真题 题解目录
拉马车在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

方法:
1.题目中已经提示用队列结构或动态数组
2. 使用队列和栈,java语言只需导入相应的类,然后调用方法即可
3. queue: offer()方法:向队列中添加元素(队尾);add()也是在队尾添加元素。offer()方法 优于 add()
4. queue:  peek(),返回队首元素,队首元素不出队 ; poll()返回队首元素,队首元素出队。
5. stack: contains()方法,判断栈中是否存在某元素
6. stack: push(), 入队  ; pop() , 出栈  
7. StringBuffer中的sppend(),向字符串末尾添加字符串/字符...
8. 引入一个Boolean值,Awin=true;A出牌,Awin=false;B出牌
9. A赢(不是全局),Awin=true; B赢 Awin=false,都不赢即交替出牌:A出牌Awin=false,B出牌 Awin=true10. 引入count,如果运行了10000次,对局仍为结束,则认为是死循环,输出-1
11. 最后将队列中字符生成字符串,出队时queue.size(),一直在变, 
    so应在遍历出队前记录,queue.size();以确定出队次数。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Main008_拉马车 {
     public static void main(String[] args) {
  	  Scanner in = new Scanner(System.in);
	  String a = in.nextLine();
	  String b = in.nextLine();
	  int len = a.length()+b.length();
	  Queue<Character> queue1 = new LinkedList<Character>();
	  Queue<Character> queue2 = new LinkedList<Character>();
	  Stack<Character> stack = new Stack<Character>();
	  // 字符串中字符的入队操作
	  for(int i=0;i<a.length();i++) 
	      queue1.offer(a.charAt(i));
	  for(int i=0;i<b.length();i++) 
	      queue2.offer(b.charAt(i));
	  Boolean Awin = true; // 初始和A赢牌时A先出牌
	  int count = 0 ;  // 计数双方一共出牌多少次,当count过大时返回 -1
	  while(!queue1.isEmpty() && !queue2.isEmpty()) {
	      count++;
	      if(count==10000)  break; // 当达到10000次,说明存在死循环,即跳出循环,返回-1
	      if(Awin==true) {  //初始和A赢牌时A先出牌
	    	  if(stack.contains(queue1.peek())) { //判断栈中是否包含队首元素
	     	      Awin = true;  //包含,则A局部获胜,Awin=true;
	     	      char t = queue1.poll(); // 记录队首元素,并出队
	              queue1.offer(t);   // 将元素添加至队尾
	              while(stack.peek()!=t)  // 将两个相同元素之间的栈中的元素添加至队尾
	           	  queue1.offer(stack.pop());
	              queue1.offer(stack.pop()); //将栈中和队首相同的元素添加中队尾
	          }else {
	     	      stack.push(queue1.poll()); //栈中不包含队首元素
	     	      Awin = false;   // Awin=false; 下次B出牌
	    	  }
	  	// 注释同上
	     }else {
	    	 if(stack.contains(queue2.peek())) {
	     	     Awin = false;   // stack中存在元素和B出牌的元素相同时,则B收牌,下次B先出牌
	             char t = queue2.poll();
	             queue2.offer(t);
	             while(stack.peek()!=t) {
	      		 queue2.offer(stack.pop());
	     	     }
	     	     queue2.offer(stack.pop());
	    	 }else {
     		     stack.push(queue2.poll());
     		     Awin = true;
    		 }
  	    }
       }
       StringBuffer sb = new StringBuffer();
       if(queue2.isEmpty()) {
   	   int size = queue1.size();
   	   for(int i=0;i<size;i++) // queue.size();每出队一次变化一次,so应提前记录
    	       sb.append(queue1.poll());
           System.out.println(sb.toString());
        }else if(queue1.isEmpty()) {
   	    int size = queue2.size();
   	    for(int i=0;i<size;i++)
    		sb.append(queue2.poll());
   	    System.out.println(sb.toString());
  	}else if(count==10000) // 循环10000次内,未分胜负,则认为死循环,输出-1
   	    System.out.println(-1);
    }
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值