763. 循环相克令

循环相克令是一个两人玩的小游戏。

令词为“猎人、狗熊、枪”,两人同时说出令词,同时做出一个动作——猎人的动作是双手叉腰;狗熊的动作是双手搭在胸前;枪的动作是双手举起呈手枪状。

双方以此动作判定输赢,猎人赢枪、枪赢狗熊、狗熊赢猎人,动作相同则视为平局。

现在给定你一系列的动作组合,请你判断游戏结果。

输入格式

第一行包含整数 T,表示共有 T 组测试数据。

接下来 T 行,每行包含两个字符串,表示一局游戏中两人做出的动作,字符串为 Hunter, Bear, Gun 中的一个,这三个单词分别代表猎人,狗熊和枪。

输出格式
如果第一个玩家赢了,则输出 Player1。

如果第二个玩家赢了,则输出 Player2。

如果平局,则输出 Tie。

数据范围
1≤N≤100
输入样例
3
Hunter Gun
Bear Bear
Hunter Bear
输出样例
Player1
Tie
Player2

AC

以前做过石头剪刀布的题目,很类似。

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int t;
    cin>>t;
    string a,b;
    
    while(t--){
        getchar();
        cin>>a>>b;
        int f=0;
        if(a=="Hunter"){
            if(b=="Bear") f=2;
            if(b=="Gun") f=1;
        }
        else if(a=="Bear"){
            if(b=="Hunter") f=1;
            if(b=="Gun") f=2;
        }
        else if(a=="Gun"){
            if(b=="Bear") f=1;
            if(b=="Hunter") f=2;
        }
        if(f==1) cout<<"Player1\n";
        else if(f==2) cout<<"Player2\n";
        else cout<<"Tie\n";
    }
}

头条文章

用string的size之差表示player1赢的三种情况
一时间我竟然不知道该说什么才好???!

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;

    string x, y;
    while (n --)
    {
        cin >> x >> y;
        int a = x.size(), b = y.size();
        if (a - b == -1 || a - b == -2 || a - b == 3)
            cout << "Player1" << endl;
        else if (a == b)
            cout << "Tie" << endl;
        else cout << "Player2" << endl;
    }

    return 0;

还有这一篇,直接判断Player1赢的条件,其他的只要不是相等就是Player2赢。
判断的时间少了一半。

#include<bits/stdc++.h>
using namespace std;
int main (){
    int t;
    cin>>t;
    while(t--){
        string a,b;
        cin>>a>>b;
        if(a==b)puts("Tie");
        else if(a=="Hunter"&&b=="Gun"||a=="Bear"&&b=="Hunter"||a=="Gun"&&b=="Bear")
        puts("Player1");
                else puts("Player2");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摆烂.MVP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值