方法一:二维数组
#include<iostream>
using namespace std;
int w[4][4];
int mmm(char x1[])
{
switch(x1[0])
{
case 'R':
return 1;
case 'S':
return 2;
case 'P':
return 3;
}
}
void mmm2()
{
w[1][1]=w[2][2]=w[3][3]=0;
w[1][2]=w[2][3]=w[3][1]=1;
w[2][1]=w[3][2]=w[1][3]=-1;
}
int main()
{
mmm2();
char S1[100],S2[100];
int n1,n2;
int N;
cin>>N;
for(int i=0;i<N;i++)
{
cin>>S1>>S2;
n1=mmm(S1);//如果S1为Rock,则S[0]为R,所有n1=1
n2=mmm(S2);
if(w[n1][n2]==0)
{
cout<<"Tie"<<endl;
}
else if(w[n1][n2]==1)
{
cout<<"Player1"<<endl;
}
else
{
cout<<"Player2"<<endl;
}
}
return 0;
}
方法二:string类
#include<iostream>
using namespace std;
int main()
{
int N;
cin>>N;
string S1;
string S2;
for(int i=1;i<=N;i++)
{
cin>>S1>>S2;
if(S1=="Rock"&&S2=="Scissors")
{
cout<<"Player1"<<endl;
}
else if(S1=="Scissors"&&S2=="Paper")
{
cout<<"Player1"<<endl;
}
else if(S1=="Paper"&&S2=="Rock")
{
cout<<"Player1"<<endl;
}
else if(S1==S2)
{
cout<<"Tie"<<endl;
}
else
{
cout<<"Player2"<<endl;
}
}
return 0;
}