石头剪刀布

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	int n;
	cin>>n;
	char s1[10],s2[10];
	for(int i=1;i<=n;i++)
	{
		cin>>s1>>s2;
		if(strcmp(s1,s2)==0)
			cout<<"Tie"<<endl;
		else if(strcmp(s1,"Rock")==0&&strcmp(s2,"Scissors")==0
			||strcmp(s1,"Scissors")==0&&strcmp(s2,"Paper")==0
			||strcmp(s1,"Paper")==0&&strcmp(s2,"Rock")==0)
			cout<<"Player1"<<endl;
		else if(strcmp(s1,"Rock")==0&&strcmp(s2,"Paper")==0
			||strcmp(s1,"Scissors")==0&&strcmp(s2,"Rock")==0
			||strcmp(s1,"Paper")==0&&strcmp(s2,"Scissors")==0)
			cout<<"Player2"<<endl;
	}
	return 0;
}

石头剪刀布

如果下面这样写,编译是通不过的.

石头剪刀布

#include<iostream>
#include<cstring>
using namespace std;

int main()
{
	int n;
	cin>>n;
	string s1,string s2;
	for(int i=1;i<=n;i++)
	{
		cin>>s1>>s2;
		if(strcmp(s1,s2)==0)
			cout<<"Tie"<<endl;
		else if(strcmp(s1,"Rock")==0&&strcmp(s2,"Scissors")==0
			||strcmp(s1,"Scissors")==0&&strcmp(s2,"Paper")==0
			||strcmp(s1,"Paper")==0&&strcmp(s2,"Rock")==0)
			cout<<"Player1"<<endl;
		else if(strcmp(s1,"Rock")==0&&strcmp(s2,"Paper")==0
			||strcmp(s1,"Scissors")==0&&strcmp(s2,"Rock")==0
			||strcmp(s1,"Paper")==0&&strcmp(s2,"Scissors")==0)
			cout<<"Player2"<<endl;
	}
	return 0;
}

原因是strcmp函数里面的参数是char的,不能是string。

相关文章: