题解 P1328 【生活大爆炸版石头剪刀布】
HRQ666
2018-03-24 23:32:17
~~辣莫多~~那么多人用if,看的老夫心累啊……我这里使用了二维数组,制作了一张结果表。虽然与if差的不太多。但要稍稍快一些,也更简单明了。表的内容与题目里的那张表一样,但是要根据推理补全原表中黑的部分。
# 手动打表时一定要细心!
废话不多说,上程序:
```cpp
//LG-P1328
//生活大爆炸版剪刀石头布
#include<iostream>
using namespace std;
int main()
{
int score[5][5];
score[0][0]=0;
score[0][1]=-1;
score[0][2]=1;
score[0][3]=1;
score[0][4]=-1;
score[1][0]=1;
score[1][1]=0;
score[1][2]=-1;
score[1][3]=1;
score[1][4]=-1;
score[2][0]=-1;
score[2][1]=1;
score[2][2]=0;
score[2][3]=-1;
score[2][4]=1;
score[3][0]=-1;
score[3][1]=-1;
score[3][2]=1;
score[3][3]=0;
score[3][4]=1;
score[4][0]=1;
score[4][1]=1;
score[4][2]=-1;
score[4][3]=-1;
score[4][4]=0;
/*到这里都是打表内容;
score[a][b]是表,里面的数字含义与题目相同;
后面(=x)的内容如下:
x=1:小A胜,
x=0:平局,
x=-1:小B胜。*/
int n,m,k;
int ans=0;
int bns=0;
cin>>n>>m>>k;
int a[m+1],b[k+1];
//—————————————
for(int i=1;i<=m;i++)
{
cin>>a[i];
}
for(int i=1;i<=k;i++)
{
cin>>b[i];
}
//以上是在输入二人的出拳循环
int anum=1;//小A的出拳指针
int bnum=1;//小B的出拳指针
for(int i=1;i<=n;i++)
{
if(score[a[anum]][b[bnum]]==1)
{
ans++;
}//小A胜
else if(score[a[anum]][b[bnum]]==-1)
{
bns++;
}//小B胜
anum++;//指针右移
bnum++;//指针右移
if(anum>m)
{
anum=1;
}//超出循环,从头开始
if(bnum>k)
{
bnum=1;
}//超出循环,从头开始
}
cout<<ans<<" "<<bns;//输出结果
return 0;
}```