循环相克令是一个两人玩的小游戏。
令词为“猎人、狗熊、枪”,两人同时说出令词,同时做出一个动作——猎人的动作是双手叉腰;狗熊的动作是双手搭在胸前;枪的动作是双手举起呈手枪状。
双方以此动作判定输赢,猎人赢枪、枪赢狗熊、狗熊赢猎人,动作相同则视为平局。
现在给定你一系列的动作组合,请你判断游戏结果。
输入格式
第一行包含整数 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;
}