1.题目:
题目描述
现在有两个人在玩石头剪子布游戏,请你判断最后谁赢了。
用R代表石头,S代表剪子,P代表布。
输入格式
输入的第一行是一个整数t(0<t<1000),表示测试样例的数目。
每组输入样例的第一行是一个整数n(0<n<100),表示游戏次数。
接下来n行,每行由两个字母组成,两个字母之间用一个空格分隔,这些字母只会是R,S或P。
第一个字母表示Player1的选择,第二个字母表示Player2的选择。
输出
对于每组输入样例,输出获胜方的名字(Player1或Player2),如果平均,则输出TIE。
样例输入
3
2
R P
S R
3
P P
R S
S R
1
P R
样例输出
Player 2
TIE
Player 1
2.代码一:
#include <iostream>
using namespace std;
int main()
{
int t, n, score1, score2;
char a, b;
cin >> t;
while (t--) {
cin >> n;
score1 = score2 = 0;
while (n--) {
cin >> a >> b;
if (a == b) {
score1++;
score2++;
} else if (a == 'R' && b == 'S')
score1++;
else if (a == 'S' && b == 'R')
score2++;
else if (a == 'P' && b == 'R')
score1++;
else if (a == 'R' && b == 'P')
score2++;
else if (a == 'S' && b == 'P')
score1++;
else if (a == 'P' && b == 'S')
score2++;
}
if (score1 > score2)
cout << "Player 1";
else if (score1 == score2)
cout << "TIE";
else if (score1 < score2)
cout << "Player 2";
cout << endl;
}
return 0;
}
代码二:
#include<stdio.h>
int main()
{
int t, n, s1, s2;
char a, b;
scanf("%d", &t);
while (t--) {
scanf("%d%*c", &n);
s1 = s2 = 0;
while (n--) {
scanf("%c %c%*c", &a, &b);
if ((a == 'R' && b == 'S') || (a == 'S' && b == 'P') || (a == 'P' && b == 'R'))
s1++;
else if ((b == 'R' && a == 'S') || (b == 'S' && a == 'P') || (b == 'P' && a == 'R'))
s2++;
else {
s1++;
s2++;
}
}
if (s1 > s2)
printf("Player 1\n");
else if (s1 < s2)
printf("Player 2\n");
else
printf("TIE\n");
}
return 0;
}