B2112题解
法1:暴力判断
依题意得,读入两个字符串进行判断(只判断首位即可)。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=0; i<n; i++) {
string a,b;
cin>>a>>b;//读入两个字符串
if(a[0]==b[0])cout<<"Tie\n";//首位相同,平局
else if(a[0]=='R') {
if(b[0]=='P')cout<<"Player2\n";
else cout<<"Player1\n";
}
else if(a[0]=='P') {
if(b[0]=='S')cout<<"Player2\n";
else cout<<"Player1\n";
}
else if(a[0]=='S') {
if(b[0]=='R')cout<<"Player2\n";
else cout<<"Player1\n";
}//以上是逐个判断,按照 Player1 的情况分类讨论
//因为讨论过平局了,可以只分两种情况考虑
//记得使用 else-if
}
return 0;
}
法2 (我也不知道叫啥)
基于法1只判断首位的做法,我们将字符串首的 ASCII 码列出:
| 石头 | 剪刀 | 布 |
|---|---|---|
| 82 | 83 | 80 |
那因为石头打剪刀,布包石头,而石头的数值小于剪刀,布又小于石头,我们可以得出:
除非是剪刀剪布(数值为83、80)的情况外,数值小的获胜!
便可得出如下代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
for(int i=0; i<n; i++) {
string a,b;
cin>>a>>b;//读入两个字符串
if(a[0]==b[0])cout<<"Tie\n";//首位相同,平局
else if(a[0]==83&&b[0]==80)cout<<"Player1\n";
else if(a[0]==80&&b[0]==83)cout<<"Player2\n";//以上两行特判剪刀和布的情况
else if(a[0]<b[0])cout<<"Player1\n";
else cout<<"Player2\n";
}
return 0;
}
以上两种方法实测可过。