1018 锤子剪刀布 (20分)
大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式: 输入第 1 行给出正整数 N(≤105),即双方交锋的次数。随后 N 行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C 代表“锤子”、J 代表“剪刀”、B 代表“布”,第 1 个字母代表甲方,第 2个代表乙方,中间有 1 个空格。
输出格式: 输出第 1、2 行分别给出甲、乙的胜、平、负次数,数字间以 1 个空格分隔。第 3 行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有 1 个空格。如果解不唯一,则输出按字母序最小的解。
输入样例:
10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J
输出样例:
5 3 2
2 3 5
B B
我们通过题目可以分析到以下条件
- 输入甲乙交锋的次数N,输入他们每次比评出的是啥,最后输出甲乙的胜,平,负的次数,还有甲乙的获胜次数最多的是什么,注意是胜平负
- 如果甲乙获胜的的最多的手势解不唯一,则按照字母序最小的输出
import java.io.*;
public class C_18 {
public static void main(String[] args) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String N=br.readLine();
String[] result;
int[] first = new int[3];
int[] second = new int[3];
int[] first_arr = new int[3];
int[] second_arr = new int[3];
for (int i = 0; i <Integer.parseInt(N); i++) {
String check =br.readLine();
result = check.split(" ");
if (result[0].equals(result[1])) {
first[2]++;
second[2]++;
}
if ((result[0].equals("C") && result[1].equals("J")) || (result[0].equals("J") && result[1].equals("B")) || (result[0].equals("B") && result[1].equals("C"))) {
first[0]++;
second[1]++;
if (result[0].equals("B"))
first_arr[0]++;
if (result[0].equals("C"))
first_arr[1]++;
if (result[0].equals("J"))
first_arr[2]++;
} else if ((result[0].equals("J") && result[1].equals("C")) || (result[0].equals("C") && result[1].equals("B")) || (result[0].equals("B") && result[1].equals("J"))) {
first[1]++;
second[0]++;
if (result[1].equals("B"))
second_arr[0]++;
if (result[1].equals("C"))
second_arr[1]++;
if (result[1].equals("J"))
second_arr[2]++;
}
}
System.out.print(first[0] + " " + first[2] + " " + first[1] + "\n");
System.out.print(second[0] + " " + second[2] + " " + second[1] + "\n");
Judge(first_arr);
System.out.print(" ");
Judge(second_arr);
}
public static void Judge(int[] arr){
int Max=0;
if(arr[0]>=arr[1]&&arr[0]>=arr[2])
Max=0;
else if(arr[1]>=arr[2]&&arr[1]>arr[0])
Max=1;
else Max=2;
switch (Max) {
case 0:
System.out.print("B");
break;
case 1:
System.out.print("C");
break;
case 2:
System.out.print("J");
break;
}
}
}
条件就像上面说的那样,我写这道题倒是发现了我的一些问题,字符串的比较不能用等于号来比较,得用equals来,因为这个我debug半天,最后才意识到,害.
这道题Java最后一个测试过不去,但我看见其他大佬用Java做出来了,他们用的方法,我不是很了解,所以就没试,待我了解一下.
最后,我是三岁啊,大佬又什么好的解决办法,欢迎评论区留言,谢谢