题目描述:
大家应该都会玩“锤子剪刀布”的游戏:现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入描述:
输入第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
Java代码:
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Model a = new Model(); Model b = new Model(); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { char jia = scanner.next().charAt(0); char yi = scanner.next().charAt(0); check(a,b,jia,yi); } String mostA = ""; String mostB = ""; if (Math.max(a.map.get("B"),Math.max(a.map.get("C"),a.map.get("J"))) == a.map.get("B")){ mostA = "B"; }else if (Math.max(a.map.get("B"),Math.max(a.map.get("C"),a.map.get("J"))) == a.map.get("C")){ mostA = "C"; }else mostA = "J"; if (Math.max(b.map.get("B"),Math.max(b.map.get("C"),b.map.get("J"))) == b.map.get("B")){ mostB = "B"; }else if (Math.max(b.map.get("B"),Math.max(b.map.get("C"),b.map.get("J"))) == b.map.get("C")){ mostB = "C"; }else mostB = "J"; System.out.println(a.toString()); System.out.println(b.toString()); System.out.println(mostA + " " + mostB); scanner.close(); } public static void check(Model a,Model b,char jia,char yi){ if (jia == 'C'){ switch (yi){ case 'C': a.tie++;b.tie++;break; case 'J': a.win++;b.lose++;a.map.put("C",a.map.get("C")+1);break; case 'B': a.lose++;b.win++;b.map.put("B",b.map.get("B")+1);break; } } else if (jia == 'J'){ switch (yi){ case 'J': a.tie++;b.tie++;break; case 'B': a.win++;b.lose++;a.map.put("J",a.map.get("J")+1);break; case 'C': a.lose++;b.win++;b.map.put("C",b.map.get("C")+1);break; } }else { switch (yi){ case 'B': a.tie++;b.tie++;break; case 'C': a.win++;b.lose++;a.map.put("B",a.map.get("B")+1);break; case 'J': a.lose++;b.win++;b.map.put("J",b.map.get("J")+1);break; } } } static class Model { int win = 0; int tie = 0; int lose = 0; Map<String,Integer> map = new HashMap<>(); public Model() { this.map.put("C",0); this.map.put("J",0); this.map.put("B",0); } @Override public String toString() { return win + " " + tie + " " + lose; } } }