1018. 鎚子剪刀布 (20)

來源:互聯網
上載者:User

標籤:java   pat   

大家應該都會玩“鎚子剪刀布”的遊戲:兩人同時給出手勢,勝負規則:

現給出兩人的交鋒記錄,請統計雙方的勝、平、負次數,並且給出雙方分別出什麼手勢的勝算最大。

輸入格式:

輸入第1行給出正整數N(<=105),即雙方交鋒的次數。隨後N行,每行給出一次交鋒的資訊,即甲、乙雙方同時給出的的手勢。C代表“鎚子”、J代表“剪刀”、B代表“布”,第1個字母代表甲方,第2個代表乙方,中間有1個空格。

輸出格式:

輸出第1、2行分別給出甲、乙的勝、平、負次數,數字間以1個空格分隔。第3行給出兩個字母,分別代表甲、乙獲勝次數最多的手勢,中間有1個空格。如果解不唯一,則輸出按字母序最小的解。

輸入範例:
10C JJ BC BB BB CC CC BJ BB CJ J
輸出範例:
5 3 22 3 5B 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;  }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.