PAT Basic 1018 锤子剪刀布 (20 分)

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

FigCJB.jpg

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

输入格式:

输入第 1 行给出正整数 N(≤),即双方交锋的次数。随后 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>
using namespace std;
bool judge(string tmp1,string tmp2){
    if(tmp1=="C"&&tmp2=="J") return true;
    if(tmp1=="C"&&tmp2=="B") return false;
    if(tmp1=="J"&&tmp2=="C") return false;
    if(tmp1=="J"&&tmp2=="B") return true;
    if(tmp1=="B"&&tmp2=="J") return false;
    if(tmp1=="B"&&tmp2=="C") return true;
}
string fun(int c,int j,int b){
    if(b>=c&&b>=j) return "B";
    if(c>=j&&c>=b) return "C";
    if(j>=b&&j>=c) return "J";
}
int main()
{
    int T;
    cin>>T;
    string tmp1,tmp2;
    int a1=0,a2=0,a3=0;
    int b1=0,b2=0,b3=0;
    int ac=0,aj=0,ab=0;
    int bc=0,bj=0,bb=0;
    while(T--){
        cin>>tmp1>>tmp2;
        if(tmp1==tmp2) {
            a2++;b2++;
        }else if(judge(tmp1,tmp2)){
            a1++;b3++;
            if(tmp1=="C") ac++;
            else if(tmp1=="J") aj++;
            else ab++;
        }else{
            a3++;b1++;
            if(tmp2=="C") bc++;
            else if(tmp2=="J") bj++;
            else bb++;
        }
    }
    cout<<a1<<" "<<a2<<" "<<a3<<endl;
    cout<<b1<<" "<<b2<<" "<<b3<<endl;
    cout<<fun(ac,aj,ab)<<" "<<fun(bc,bj,bb)<<endl;
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/littlepage/p/11406818.html