1 条题解
-
0
自动搬运
来自洛谷,原作者为

ofbwyx
CSP-J 2020 RP++搬运于
2025-08-24 21:07:23,当前版本为作者最后更新于2021-07-03 20:26:44,作者可能在搬运后再次修改,您可在原文处查看最新版自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多
以下是正文
法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; }以上两种方法实测可过。
- 1
信息
- ID
- 6969
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者