石头剪子布
💐The Begin💐点点关注,收藏不迷路💐
|
石头剪子布,是一种猜拳游戏。起源于中国,然后传到日本、朝鲜等地,随着亚欧贸易的不断发展它传到了欧洲,到了近现代逐渐风靡世界。简单明了的规则,使得石头剪子布没有任何规则漏洞可钻,单次玩法比拼运气,多回合玩法比拼心理博弈,使得石头剪子布这个古老的游戏同时用于“意外”与“技术”两种特性,深受世界人民喜爱。
游戏规则:石头打剪刀,布包石头,剪刀剪布。
现在,需要你写一个程序来判断石头剪子布游戏的结果。
输入
输入包括N+1行:
第一行是一个整数N,表示一共进行了N次游戏。1 <= N <= 100。
接下来N行的每一行包括两个字符串,表示游戏参与者Player1,Player2的选择(石头、剪子或者是布):
S1 S2
字符串之间以空格隔开S1,S2只可能取值在{“Rock”, “Scissors”, “Paper”}(大小写敏感)中。
输出
输出包括N行,每一行对应一个胜利者(Player1或者Player2),或者游戏出现平局,则输出Tie。
样例输入
3
Rock Scissors
Paper Paper
Rock Paper
样例输出
Player1
Tie
Player2
提示
Rock是石头,Scissors是剪刀,Paper是布。
C语言实现
#include <stdio.h>
#include <string.h>
#define MAX_N 100
int main() {
int n;
scanf(“%d”, &n); // 读取游戏进行的次数N
char player1[MAX_N][10];
char player2[MAX_N][10];
for (int i = 0; i < n; i++) {
scanf("%s %s", player1[i], player2[i]); // 读取每一轮两个玩家的选择
}
for (int i = 0; i < n; i++) {
if (strcmp(player1[i], "Rock") == 0 && strcmp(player2[i], "Scissors") == 0) {
printf("Player1\n"); // 石头赢剪刀
} else if (strcmp(player1[i], "Scissors") == 0 && strcmp(player2[i], "Paper") == 0) {
printf("Player1\n"); // 剪刀赢布
} else if (strcmp(player1[i], "Paper") == 0 && strcmp(player2[i], "Rock") == 0) {
printf("Player1\n"); // 布赢石头
} else if (strcmp(player1[i], "Scissors") == 0 && strcmp(player2[i], "Rock") == 0) {
printf("Player2\n"); // 剪刀输石头
} else if (strcmp(player1[i], "Paper") == 0 && strcmp(player2[i], "Scissors") == 0) {
printf("Player2\n"); // 布输剪刀
} else if (strcmp(player1[i], "Rock") == 0 && strcmp(player2[i], "Paper") == 0) {
printf("Player2\n"); // 石头输布
} else {
printf("Tie\n"); // 平局情况
}
}
return 0;
}
C++实现
#include <iostream
>
#include <string
>
using namespace std;
int main() {
int n;
cin >> n; // 读取游戏进行的次数N
string player1[100];
string player2[100];
for (int i = 0; i < n; i++) {
cin >> player1[i] >> player2[i]; // 读取每一轮两个玩家的选择
}
for (int i = 0; i < n; i++) {
if (player1[i] == "Rock" && player2[i] == "Scissors") {
cout << "Player1\n"; // 石头赢剪刀
} else if (player1[i] == "Scissors" && player2[i] == "Paper") {
cout << "Player1\n"; // 剪刀赢布
} else if (player1[i] == "Paper" && player2[i] == "Rock") {
cout << "Player1\n"; // 布赢石头
} else if (player1[i] == "Scissors" && player2[i] == "Rock") {
cout << "Player2\n"; // 剪刀输石头
} else if (player1[i] == "Paper" && player2[i] == "Scissors") {
cout << "Player2\n"; // 布输剪刀
} else if (player1[i] == "Rock" && player2[i] == "Paper") {
cout << "Player2\n"; // 石头输布
} else {
cout << "Tie\n"; // 平局情况
}
}
return 0;
}
Java实现
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(); // 读取游戏进行的次数N
String[] player1 = new String[n];
String[] player2 = new String[n];
for (int i = 0; i < n; i++) {
player1[i] = scanner.next();
player2[i] = scanner.next(); // 读取每一轮两个玩家的选择
}
for (int i = 0; i < n; i++) {
if ("Rock".equals(player1[i]) && "Scissors".equals(player2[i])) {
System.out.println("Player1"); // 石头赢剪刀
} else if ("Scissors".equals(player1[i]) && "Paper".equals(player2[i])) {
System.out.println("Player1"); // 剪刀赢布
} else if ("Paper".equals(player1[i]) && "Rock".equals(player2[i])) {
System.out.println("Player1"); // 布赢石头
} else if ("Scissors".equals(player1[i]) && "Rock".equals(player2[i])) {
System.out.println("Player2"); // 剪刀输石头
} else if ("Paper".equals(player1[i]) && "Scissors".equals(player2[i])) {
System.out.println("Player2"); // 布输剪刀
} else if ("Rock".equals(player1[i]) && "Paper".equals(player2[i])) {
System.out.println("Player2"); // 石头输布
} else {
System.out.println("Tie"); // 平局情况
}
}
scanner.close();
}
}
Python实现
n = int(input()) # 读取游戏进行的次数N
for _ in range(n):
player1, player2 = input().split() # 读取每一轮两个玩家的选择
if player1 == "Rock" and player2 == "Scissors":
print("Player1") # 石头赢剪刀
elif player1 == "Scissors" and player2 == "Paper":
print("Player1") # 剪刀赢布
elif player1 == "Paper" and player2 == "Rock":
print("Player1") # 布赢石头
elif player1 == "Scissors" and player2 == "Rock":
print("Player2") # 剪刀输石头
elif player1 == "Paper" and player2 == "Scissors":
print("Player2") # 布输剪刀
elif player1 == "Rock" and player2 == "Paper":
print("Player2") # 石头输布
else:
print("Tie") # 平局情况
💐The End💐点点关注,收藏不迷路💐
|
原文地址:https://blog.csdn.net/qq_41840843/article/details/144174454
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!