Question

星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌平均分成两份,每人拿一份。小哼先拿出手中的第一张扑克牌放在桌上,然后小哈也拿出手中的第一张扑克牌,并放在小哼刚打出的扑克牌的上面,就像这样两人交替出牌。出牌时,如果某人打出的牌与桌上某张牌的牌面相同,即可将两张相同的牌及其中间所夹的牌全部取走,并依次放到自己手中牌的末尾。当任意一人手中的牌全部出完时,游戏结束,对手获胜。
假如游戏开始时,小哼手中有6 张牌,顺序为2 4 1 2 5 6,小哈手中也有6 张牌,顺序为3 1 3 5 6 4,最终谁会获胜呢?现在你可以拿出纸牌来试一试。接下来请你写一个程序来自动判断谁将获胜。这里我们做一个约定,小哼和小哈手中牌的牌面只有1~9。

Analysis

我们先来分析一下这个游戏有哪几种操作。小哼有两种操作,分别是出牌和赢牌。这恰好对应队列的两个操作,出牌就是出队,赢牌就是入队。小哈的操作和小哼是一样的。而桌子就是一个栈,每打出一张牌放到桌上就相当于入栈。当有人赢牌的时候,依次将牌从桌上拿走,这就相当于出栈。那如何解决赢牌的问题呢?赢牌的规则是:如果某人打出的牌与桌上的某张牌相同,即可将两张牌以及中间所夹的牌全部取走。那如何知道桌上已经有哪些牌了呢?最简单的方法就是枚举桌上的每一张牌,当然也有更好的办法我们待会再说。OK,小结一下,我们需要两个队列、一个栈来模拟整个游戏。

Input & Output

[Input Example]

  1. 2 4 1 2 5 6
  2. 3 1 3 5 6 4

[Output Example]

  1. 小哼win
  2. 小哼手中的牌是:
  3. 5 6 2 3 1 4 6 5
  4. 桌上的牌是:
  5. 2 1 3 4

Solution

  1. import java.util.Scanner;
  2. public class card_fishing
  3. {
  4. static int N = 12;
  5. static int[] queue1 = new int[12]; //小哼手中的牌
  6. static int head1 = 0, tail1 = 0;
  7. static int[] queue2 = new int[12]; //小哈手中的牌
  8. static int head2 = 0, tail2 = 0;
  9. static int[] stack = new int[9]; //桌上的牌
  10. static int top = 0;
  11. static boolean[] book = new boolean[10]; //标记哪些牌在桌上
  12. public static void main(String[] args)
  13. {
  14. Scanner sc = new Scanner(System.in);
  15. //小哼手中的牌
  16. for(int i=0; i<6; i++)
  17. {
  18. queue1[tail1] = sc.nextInt();
  19. tail1 = (tail1 + 1) % N;
  20. }
  21. //小哈手中的牌
  22. for(int i=0; i<6; i++)
  23. {
  24. queue2[tail2] = sc.nextInt();
  25. tail2 = (tail2 + 1) % N;
  26. }
  27. sc.close();
  28. while(head1!=tail1 && head2!=tail2)
  29. {
  30. //小哼出牌
  31. int temp = queue1[head1];
  32. //桌上没有这张牌
  33. if(!book[temp])
  34. {
  35. //出队、入栈
  36. head1 = (head1 + 1) % N;
  37. stack[++top] = temp;
  38. book[temp] = true;
  39. }
  40. else
  41. {
  42. //出队、入队
  43. head1 = (head1 + 1) % N;
  44. queue1[tail1] = temp;
  45. tail1 = (tail1 + 1) % N;
  46. //出栈、入队
  47. while(stack[top] != temp)
  48. {
  49. book[stack[top]] = false;
  50. queue1[tail1] = stack[top];
  51. tail1 = (tail1 + 1) % N;
  52. top--;
  53. }
  54. }
  55. temp = queue2[head2];
  56. if(!book[temp])
  57. {
  58. head2 = (head2 + 1) % N;
  59. stack[++top] = temp;
  60. book[temp] = true;
  61. }
  62. else
  63. {
  64. head2 = (head2 + 1) % N;
  65. queue2[tail2] = temp;
  66. tail2 = (tail2 + 1) % N;
  67. while(stack[top] != temp)
  68. {
  69. book[stack[top]] = false;
  70. queue2[tail2] = stack[top];
  71. tail2 = (tail2 + 1) % N;
  72. top--;
  73. }
  74. }
  75. }
  76. if(head2 == tail2)
  77. {
  78. System.out.println("小哼win");
  79. System.out.println("小哼手中的牌是:");
  80. for(int i=head1; i!=tail1; i=(i+1)%N)
  81. System.out.print(queue1[i] + " ");
  82. System.out.println();
  83. if(top > 0) //如果桌上有牌则依次输出桌上的牌
  84. {
  85. System.out.println("桌上的牌是:");
  86. for(int i=1;i<=top;i++)
  87. System.out.print(stack[i] + " ");
  88. }
  89. else
  90. System.out.println("桌上已经没有牌了");
  91. }
  92. else
  93. {
  94. System.out.println("小哈win");
  95. System.out.println("小哈手中的牌是:");
  96. for(int i=head2; i!=tail2; i=(i+1)%N)
  97. System.out.print(queue2[i] + " ");
  98. System.out.println();
  99. if(top > 0) //如果桌上有牌则依次输出桌上的牌
  100. {
  101. System.out.println("桌上的牌是:");
  102. for(int i=1;i<=top;i++)
  103. System.out.print(stack[i] + " ");
  104. }
  105. else
  106. System.out.println("桌上已经没有牌了");
  107. }
  108. }
  109. }