PAT乙級1018.鎚子剪刀布 (20)(20 分)

來源:互聯網
上載者:User

標籤:alt   max   技術分享   http   次數   int   ==   har   use   

1018 鎚子剪刀布 (20)(20 分)

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

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

輸入格式:

輸入第1行給出正整數N(<=10^5^),即雙方交鋒的次數。隨後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

#include<iostream>#include<stdlib.h>using namespace std;int findNum(char a ,char *code){    for (int i = 0; i < 3; i++)    {        if (code[i] == a)            return i;    }}int main(){    int N;//共幾回合遊戲    char code[3] = {‘B‘, ‘C‘, ‘J‘};//每種手形的編號    int chess[3][3] = {{0, 1, -1}, {-1, 0 , 1}, {1, -1 , 0 }};//每種情形甲的輸贏情況    int result[2][3];//記錄甲乙勝平負的數量    int win[2][3];//記錄甲乙贏的回合中各種手形的次數    for (int i = 0; i < 2; i++)    {        for (int j = 0; j < 3; j++)        {            result[i][j] = 0;            win[i][j] = 0;        }    }    cin >> N;    while (N--)    {        char a, b;        cin >> a >> b;        int m = findNum(a, code);        int n = findNum(b, code);        if (chess[m][n] == 1)        {            result[0][0]++;            result[1][2]++;            win[0][m]++;        }        else if (chess[m][n] == 0)        {            result[0][1]++;            result[1][1]++;        }        else        {            result[0][2]++;            result[1][0]++;            win[1][n]++;        }    }    /*輸出每雙方每種結果的次數*/    for (int i = 0; i < 2; i++)    {        for (int j = 0; j < 3; j++)        {            cout << result[i][j];            if (j == 2)                cout << endl;            else                cout << " ";        }    }    /*找出贏的次數最多的手形*/    int x[2] = { 0, 0 };    for (int i = 0; i < 2; i++)    {        int max = 0;        for (int j = 0; j < 3; j++)        {            if (win[i][j] > max)            {                max = win[i][j];                x[i] = j;            }        }    }    cout << code[x[0]] << " " << code[x[1]] << endl;    system("pause");    return 0;}

 

PAT乙級1018.鎚子剪刀布 (20)(20 分)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.