锤子剪刀布 (20)
得出A和B所有手势赢的次数,然后求平局次数后输出。最后的赢的次数最多的手势因为只有三个直接判断就行。
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int ABwin = 0, ACwin =0, AJwin =0;//A出各剪刀锤子布赢得次数 int BBwin = 0, BCwin =0, BJwin =0;//B出各剪刀锤子布赢得次数 int N =sc.nextInt();//双方交锋的次数 for(int i=0; i<N; i++){ String A = sc.next(); String B = sc.next(); if(A.equals("B") && B.equals("C")) ABwin++;// A出布B,B出锤子C,A赢一次 else if (A.equals("C") && B.equals("J")) ACwin++; else if (A.equals("J") && B.equals("B")) AJwin++; else if (B.equals("B") && A.equals("C")) BBwin++; else if (B.equals("C") && A.equals("J")) BCwin++; else if (B.equals("J") && A.equals("B")) BJwin++; } int Acount = ABwin + ACwin + AJwin; // A赢的总次数 int Bcount = BBwin + BCwin + BJwin;// B赢的总次数 int PJcount = N - (Acount + Bcount); // 平局的次数:总次数 -( A赢的次数 +- 和B赢的次数) System.out.println(Acount + " " + PJcount + " " + Bcount); System.out.println(Bcount + " " + PJcount + " " + Acount); System.out.println(max(ABwin, ACwin, AJwin) + " "+ max(BBwin, BCwin, BJwin)); } /** @return:赢的次数最大手势 */ public static char max(int B, int C, int J){ if(B>=C && B>=J) return 'B';//B大于等于C和J if(C >B && C>=J) return 'C';//比B大,且大于等于J return 'J'; } }