PTA刷题Basic篇——1018.锤子剪刀布——Day(9)

题目描述

在这里插入图片描述在这里插入图片描述

题目分析

设立一个比较函数来判断A赢或者 B赢或者平局。分别计算出现的次数,然后输出。对于获胜最多手势,我们求出两个人在赢的情况下,分别是以什么手势赢的,取最大值对应的手势输出。但是这里要注意的是,我们先判断这个最大值和对应手势的出现次数的比较顺序应该是按照字母降序的,即B->C->J的比较顺序。因为如果遇到相等的次数,我们就可以先将小字母先输出,其他的就不用看了。

代码

#include <iostream>
using namespace std;
int cmp(char c1,char c2)
{
    if((c1 == 'B' && c2 == 'C') || (c1 == 'C' && c2 == 'J') || (c1 == 'J' && c2 == 'B'))
        return 1;
    else if((c1 == 'C' && c2 == 'B') || (c1 == 'J' && c2 == 'C') || (c1 == 'B' && c2 == 'J'))
        return 2;
    else
        return 0;
}
int main()
{
    int n;
    cin>>n;//»ØºÏÊý
    char c1,c2;
    int count1 = 0,count2 = 0,count0 = 0;
    int count1b = 0,count1j = 0,count1c = 0;
    int count2b = 0,count2j = 0,count2c = 0;
    for(int i = 0;i < n;i++)
    {
        cin>>c1>>c2;
        if(cmp(c1,c2)==1)
        {
            if(c1=='B')count1b+=1;
            else if(c1=='C')count1c+=1;
            else count1j+=1;
            count1+=1;
        }
        else if(cmp(c1,c2) == 2)
        {
            if(c2=='B')count2b+=1;
            else if(c2=='C')count2c+=1;
            else count2j+=1;
            count2+=1;
        }
        else
            count0+=1;
    }
    cout<<count1<<" "<<count0<<" "<<count2<<endl;
    cout<<count2<<" "<<count0<<" "<<count1<<endl;
    int answer1 = max(max(count1b,count1c),count1j);
    int answer2 = max(max(count2b,count2c),count2j);
    if(answer1 == count1b)
        cout<<'B'<<" ";
    else if(answer1 == count1c)
        cout<<'C'<<" ";
    else
        cout<<'J'<<" ";
    if(answer2 == count2b)
        cout<<'B';
    else if(answer2 == count2c)
        cout<<'C';
    else
        cout<<'J';
    return 0;
}

总结

答题用时12min
Q18——finish√

发布了60 篇原创文章 · 获赞 2 · 访问量 1060

猜你喜欢

转载自blog.csdn.net/weixin_44755413/article/details/105548538