大家应该都会玩“锤子剪刀布”的游戏:
现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代
表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯
一,则输出按字母序最小的解。
10<br/>C J<br/>J B<br/>C B<br/>B B<br/>B C<br/>C C<br/>C B<br/>J B<br/>B C<br/>J J
5 3 2<br/>2 3 5<br/>B B
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /*输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息, 即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代 表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。*/ public class Main{ public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); int vict = 0, fail = 0, same = 0; String[] str = new String[]{"B","C","J"}; int[] count1 = new int[3]; int[] count2 = new int[3]; for (int i = 0; i < n; i++) { String[] s = reader.readLine().split(" "); if (s[0].equals(s[1])) { same++; } else if (("C".equals(s[0]) && "J".equals(s[1])) || ("J".equals(s[0]) && "B".equals(s[1])) || ("B".equals(s[0]) && "C".equals(s[1]))) { vict++; for(int k = 0;k < 3;k++){ if(s[0].equals(str[k])) count1[k]++; } } else { fail++; for(int k = 0;k < 3;k++){ if(s[1].equals(str[k])) count2[k]++; } } } reader.close(); System.out.println(vict + " " + same + " " + fail); System.out.println(fail + " " + same + " " + vict); System.out.println(str[get(count1)] + " " + str[get(count2)]); }i private static int get(int[] arr){ int index = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > arr[index]) { index = i; } } return index; } }
import java.util.*; public clas***ain { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); String[][] array = new String[n][2]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[0].length; j++) { array[i][j] = scanner.next(); } } // 分别代表BCJ int[] array2 = {0, 0, 0}; int[] array3 = {0, 0, 0}; int pingju = 0; for (int i = 0; i < array.length; i++) { if (array[i][0].equals(array[i][1])) { pingju++; } else if ((array[i][0].equal***") && array[i][1].equals("C"))) { array2[0]++; } else if ((array[i][0].equals("C") && array[i][1].equals("J"))) { array2[1]++; } else if (array[i][0].equals("J") && array[i][1].equal***")) { array2[2]++; } else if (array[i][0].equal***") && array[i][1].equals("J")) { array3[2]++; } else if ((array[i][0].equals("J") && array[i][1].equals("C"))) { array3[1]++; } else if ((array[i][0].equals("C") && array[i][1].equal***"))) { array3[0]++; } } int jia_win = 0; for (int i = 0; i < array2.length; i++) { jia_win += array2[i]; } System.out.print(jia_win); System.out.print(" " + pingju + " "); System.out.println(n - jia_win - pingju); System.out.print(n - jia_win - pingju); System.out.print(" " + pingju + " "); System.out.println(jia_win); // System.out.println(Arrays.toString(array2)); // System.out.println(Arrays.toString(array3)); HashMap<String, Integer> map = new HashMap<>(); map.put("B", array2[0]); map.put("C", array2[1]); map.put("J", array2[2]); HashMap<String, Integer> map2 = new HashMap<>(); map2.put("B", array3[0]); map2.put("C", array3[1]); map2.put("J", array3[2]); ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); ArrayList<Map.Entry<String, Integer>> list2 = new ArrayList<>(map2.entrySet()); Collections.sort(list2, new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); // System.out.println(map); // System.out.println(list); // for (Map.Entry<String ,Integer> mapping:list // ) { // System.out.println(mapping.getKey()+" "+mapping.getValue()); // } String first = null; if (list.get(2).getValue() == list.get(1).getValue() && list.get(1).getValue() == list.get(0).getValue()) { first = list.get(0).getKey(); } else if (list.get(2).getValue() == list.get(1).getValue()) { first = list.get(1).getKey(); } else { first = list.get(2).getKey(); } String second = null; if (list2.get(2).getValue() == list2.get(1).getValue() && list2.get(1).getValue() == list2.get(0).getValue()) { second = list2.get(0).getKey(); } else { second = list2.get(2).getKey(); } System.out.print(first + " " + second); } }
import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int v = 0; int f = 0; Map<Character, Integer> mapA = new TreeMap<>(); Map<Character, Integer> mapB = new TreeMap<>(); for (int i = 0; i < n; i++) { char a = sc.next().charAt(0); char b = sc.next().charAt(0); int re = match(a, b); if (re == 1) { v++; if (mapA.containsKey(a)) { mapA.put(a, mapA.get(a) + 1); } else { mapA.put(a, 1); } } else if (re == -1) { f++; if (mapB.containsKey(b)) { mapB.put(b, mapB.get(b) + 1); } else { mapB.put(b, 1); } } } System.out.println(v + " " + (n - v - f) + " " + f); System.out.println(f + " " + (n - v - f) + " " + v); int max = 0; for (Map.Entry<Character, Integer> m : mapA.entrySet()) { if (m.getValue() > max) { max = m.getValue(); } } if (max == 0) { System.out.print("B"); } else { for (Map.Entry<Character, Integer> m : mapA.entrySet()) { if (m.getValue() == max) { System.out.print(m.getKey()); break; } } } max = 0; System.out.print(" "); for (Map.Entry<Character, Integer> m : mapB.entrySet()) { if (m.getValue() > max) { max = m.getValue(); } } if (max == 0) { System.out.print("B"); } else { for (Map.Entry<Character, Integer> m : mapB.entrySet()) { if (m.getValue() == max) { System.out.print(m.getKey()); break; } } } } } public static int match(char a, char b) { if (a == b) { return 0; } else if ((a == 'C' && b == 'J') || (a == 'J' && b == 'B') || (a == 'B' && b == 'C')) { return 1; } else { return -1; } } }
import java.util.Scanner;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { private static int getMaxIndex(int[] arr) { int maximumIndex = 0; for(int i = 0; i < arr.length; i++) { if(arr[i] > arr[maximumIndex]) { maximumIndex = i; } } return maximumIndex; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); final String[] gesture = {"B" ,"C", "J"}; int[] ATime = {0, 0, 0}; int[] BTime = {0, 0, 0}; int[] AWinGesture = {0, 0, 0}; int[] BWinGesture = {0, 0, 0}; String[] game; for(int i = 0; i < N; i++) { game = br.readLine().split(" "); switch(game[0]) { case "C" :{ switch (game[1]) { case "C":{ ATime[1]++; BTime[1]++; break; } case "J":{ ATime[0]++; BTime[2]++; AWinGesture[1]++; break; } case "B":{ ATime[2]++; BTime[0]++; BWinGesture[0]++; break; } default:break; } break; } case "J" :{ switch (game[1]) { case "C":{ ATime[2]++; BTime[0]++; BWinGesture[1]++; break; } case "J":{ ATime[1]++; BTime[1]++; break; } case "B":{ ATime[0]++; BTime[2]++; AWinGesture[2]++; break; } default:break; } break; } case "B" :{ switch (game[1]) { case "C":{ ATime[0]++; BTime[2]++; AWinGesture[0]++; break; } case "J":{ ATime[2]++; BTime[0]++; BWinGesture[2]++; break; } case "B":{ ATime[1]++; BTime[1]++; break; } default:break; } break; } default : break; } } System.out.println(ATime[0] + " " + ATime[1] + " " + ATime[2]); System.out.println(BTime[0] + " " + BTime[1] + " " + BTime[2]); System.out.println(gesture[getMaxIndex(AWinGesture)] + " " + gesture[getMaxIndex(BWinGesture)]); } }