1018. 锤子剪刀布 (20)

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
#include <iostream>
#include <cstdio>
using namespace std;

int win_num[3]={0,0,0};/*B C J*/
int neg_num[3]={0,0,0};/*B C J*/

char get_max(int arr[])
{
    int max_num=arr[0];
    int index=0;
    for(int i=1;i<3;i++)
    {
        if(arr[i]>max_num)
        {
            max_num=arr[i];
            index=i;
        }
    }
    switch(index)
    {
        case 0:
            return 'B';
        case 1:
            return 'C';
        default :
            return 'J';
    }
}

void add(int a[],char order)
{
    switch(order)
    {
        case 'B':
            a[0]++;
            break;
        case 'C':
            a[1]++;
            break;
        case 'J':
            a[2]++;
            break;
    }
}

int main()
{
    int n;
    cin>>n;
    int win=0;
    int equ=0;
    int neg=0;

    while(n--)
    {
        char a,b;
        cin>>a>>b;
        if((a=='C'&&b=='J')||(a=='J'&&b=='B')||(a=='B'&&b=='C'))
        {
            win++;
            add(win_num,a);
        }
        else if(a==b)
        {
            equ++;
        }
        else
        {
            neg++;
            add(neg_num,b);
        }
    }
    printf("%d %d %d
",win,equ,neg);
    printf("%d %d %d
",neg,equ,win);
    printf("%c %c
",get_max(win_num),get_max(neg_num));
    return 0;
}
原文地址:https://www.cnblogs.com/xiongmao-cpp/p/6370223.html