首页 > 试题广场 >

锤子剪刀布 (20)

[编程题]锤子剪刀布 (20)
  • 热度指数:31553 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
大家应该都会玩“锤子剪刀布”的游戏:

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入描述:
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代
表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。


输出描述:
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯
一,则输出按字母序最小的解。
示例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;
    }
}

编辑于 2021-01-26 13:14:22 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int awC = 0,awB =0,awJ = 0;
        int bwC = 0,bwB = 0,bwJ = 0;
        int pin = 0;
        int n = sc.nextInt();
        for(int i = 0;i<n;i++){
            String a = sc.next();
            String b = sc.next();
            if(a.equals("C")&&b.equals("J")){
                awC++;
            }else if(a.equals("J")&&b.equals("B")){
                awJ++;
            }else if(a.equals("B")&&b.equals("C")){
                awB++;
            }else if(a.equals("J")&&b.equals("C")){
                bwC++;
            }else if(a.equals("B")&&b.equals("J")){
                bwJ++;
            }else if(a.equals("C")&&b.equals("B")){
                bwB++;
            }
            else if(a.equals("C")&&b.equals("C")||a.equals("J")&&b.equals("J")||a.equals("B")&&b.equals("B")){
                pin++;
            }else {
                System.out.println("错误");
            }
        }
        int aw = awB+awC+awJ;
        int bw = bwB+bwC+bwJ;
        int ad = n-aw-pin;
        int bd = n-bw-pin;
        System.out.println(aw+" "+pin+" "+ad);
        System.out.println(bw+" "+pin+" "+bd);
        max(awB,awC ,awJ );
        System.out.print(" ");
        max(bwB,bwC ,bwJ );
    }
    private static void max(int B,int C,int J){
        if(B>C&&B>J){
            System.out.print("B");
        }else if(J>C&&J>B){
            System.out.print("J");
        }else if(C>B&&C>J){
            System.out.print("C");
        }else if((B==C&&B>J)||(B==C&&C==J)||(B==J&&B>C)){
           System.out.print("B");
        }else if(C==J&&C>B){
            System.out.print("C");
        }else {
            System.out.print("错误");
        }
    }
}

发表于 2020-03-04 17:39:21 回复(0)
import java.util.Scanner;

public clas***ain {
public static void main(String[] arg***r /> {
int n;
String str_1,str_2;
int win=0,ping=0,fu=0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
char[][] ch = new char[2][30];
for(int i=1;i<=n;i++)
{
str_1 = sc.next();
str_2 = sc.next();
if(result(str_1,str_2)==1)
{
ch[0][str_1.charAt(0)-'A']++;
win++;
}
else if(result(str_1,str_2)==0)
ping++;
else if(result(str_1,str_2)==-1)
{
ch[1][str_2.charAt(0)-'A']++;
fu++;
}
}
System.out.println(win+" "+ping+" "+fu);
System.out.println(fu+" "+ping+" "+win);
int max_a = 0;
int max_b = 0;
char a='B';
char b='B';
for(int i=0;i<=27;i++)
{
if(max_a {
max_a = ch[0][i];
a = (char) ('A' + i);
}
if(max_b {
max_b = ch[1][i];
b = (char) ('A' + i);
}
}
System.out.println(a+" "+b);
}
public static int result(String a, String b)
{
if(a.equals("C")&&b.equals("J")||a.equals("J")&&b.equal***")||a.equal***")&&b.equals("C"))
return 1;
else if(a.equal***))
return 0;
else
return -1;
}
}
发表于 2019-05-22 11:51:47 回复(0)

终于可以AC!!!

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);
    }
}

发表于 2019-05-16 13:47:38 回复(0)
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;
        }
    }
}

编辑于 2020-03-10 21:27:53 回复(1)
 import java.util.Scanner;
public class Main{
     private static int shi(char a,char b) {
      int p=0;
      if((a=='B'&&b=='C')||(a=='C'&&b=='J')||(a=='J'&&b=='B')) {
       p=1;
      }
      if((b=='B'&&a=='C')||(b=='C'&&a=='J')||(b=='J'&&a=='B')) {
       p=-1;
      }
  return p;
     }
     private static void da(int b1,int c1,int j1) {
      if(c1>j1) {
          if(c1>b1) {
           System.out.print("C");
          }
          else {
           System.out.println("B");
          }
         }
         else if(c1<j1){
          if(j1>b1) {
           System.out.print("J");
          }
          else {
           System.out.print("B");
          }
         }
         else {
          if(c1>b1) {
           System.out.print("C");
          }
          else {
           System.out.print("B");
          }
         }
     }
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  while(in.hasNext()) {
      int n=in.nextInt();
      char[][]nn=new char[n][2];
      for(int i=0;i<n;i++) {
       for(int j=0;j<2;j++) {
        nn[i][j]=in.next().charAt(0);
       }
      }
      int []num=new int[3];
      int []num1=new int[3];
      int b1=0;
       int c1=0;
       int j1=0;
       int b2=0;
       int c2=0;
       int j2=0;
            for(int i=0;i<n;i++) {
             if(shi(nn[i][0],nn[i][1])==1) {
              num[0]++;
              num1[2]++;
              if(nn[i][0]=='B')b1++;
              if(nn[i][0]=='C')c1++;
              if(nn[i][0]=='J')j1++;
             }
             else if(shi(nn[i][0],nn[i][1])==0) {
              num[1]++;
              num1[1]++;
             }
             else {
              num[2]++;
              num1[0]++;
              if(nn[i][1]=='B')b2++;
               if(nn[i][1]=='C')c2++;
              if(nn[i][1]=='J')j2++;
             }
            }
            for(int s:num) {
             System.out.print(s+" ");
            }
            System.out.println();
            for(int s:num1) {
             System.out.print(s+" ");
            }
            System.out.println();
            da(b1,c1,j1);
            System.out.print(" ");
            da(b2,c2,j2);
 }
}
}
发表于 2019-03-09 16:20:37 回复(0)
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)]);
    }
}

发表于 2017-11-15 18:07:54 回复(0)