1018. 锤子剪刀布 (20)

大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示:

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

输入格式:

输入第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;
  }

}

#include <iostream>
#include <algorithm>
using namespace std;

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;
}
char maxChar(int *chArray) {
    int max = *max_element(chArray, chArray + 3);
    if(chArray[0] == max)
        return 'B';
    if(chArray[1] == max)
        return 'C';
    return 'J';
}
int mapping(char c) {
    switch(c) {
        case 'B': return 0; break;
        case 'C':return 1; break;
        case 'J':return 2; break;
    }
}

int main() {
    int n;
    char a, b;
    int iCountWin = 0;
    int iCountEven = 0;
    int cnt1[3] = {0, 0, 0};
    int cnt2[3] = {0, 0, 0};
    while(cin>>n) {
        int m = n;
        while(m--) {
            cin>>a>>b;
            int ret = comp(a, b);
            if(1 == ret) {
                iCountWin++;
                cnt1[mapping(a)]++;
            } else if(0 == ret) {
                iCountEven++;
            } else {
                cnt2[mapping(b)]++;
            }
        }
        cout<<iCountWin<<" "<<iCountEven<<" "<<n - iCountEven - iCountWin<<endl;
        cout<<n - iCountEven - iCountWin<<" "<<iCountEven<<" "<<iCountWin<<endl;
        cout<<maxChar(cnt1)<<" "<<maxChar(cnt2)<<endl;
    }
    return 0;
}


你可能感兴趣的:(java,pat)