大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入格式:
输入第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
import java.util.Arrays; import java.util.Scanner; /** * @author jwang1 Success Factors */ public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); char a, b; int iCountWin = 0; int iCountEven = 0; int[] cnt1 = new int[] { 0, 0, 0 }; int[] cnt2 = new int[] { 0, 0, 0 }; int n = cin.nextInt(); for (int i = 0; i < n; i++) { a = cin.next().charAt(0); b = cin.next().charAt(0); int ret = comp(a, b); if (1 == ret) { iCountWin++; cnt1[mapping(a)]++; } else if (0 == ret) { iCountEven++; } else { cnt2[mapping(b)]++; } } System.out.println(iCountWin + " " + iCountEven + " " + (n - iCountEven - iCountWin) + "\n" + (n - iCountEven - iCountWin) + " " + iCountEven + " " + iCountWin + "\n" + maxChar(cnt1) + " " + maxChar(cnt2)); } public static char maxChar(int[] chArray) { int max = Integer.MIN_VALUE; for (int i = 0; i < chArray.length; i++) { if (chArray[i] > max) { max = chArray[i]; } } if (chArray[0] == max) return ‘B‘; if (chArray[1] == max) return ‘C‘; return ‘J‘; } public static int comp(char a, char b) { if (a == b) return 0; if ((‘C‘ == a && ‘J‘ == b) || (‘J‘ == a && ‘B‘ == b) || (‘B‘ == a && ‘C‘ == b)) return 1; return -1; } public static int mapping(char c) { int result = -1; switch (c) { case ‘B‘: result = 0; break; case ‘C‘: result = 1; break; case ‘J‘: result = 2; break; } return result; } }
1018. 锤子剪刀布 (20),布布扣,bubuko.com
原文地址:http://blog.csdn.net/jason_wang1989/article/details/25704879