我在解决一个问题,它说:
In Big Bang Theory, Sheldon and Raj created a new game: "rock-paper-scissors-lizard-Spock".
The rules of the game are:
- scissors cuts paper;
- paper covers rock;
- rock crushes lizard;
- lizard poisons Spock;
- Spock smashes scissors;
- scissors decapitates lizard;
- lizard eats paper;
- paper disproves Spock;
- Spock vaporizes rock;
- rock crushes scissors.
In the case of Sheldon's victory, he would've said: "Bazinga!"; if Raj had won, Sheldon would declare: "Raj cheated"; in ties, he would request a new game: "Again!". Given the options chosen by both, make a program that prints Sheldon reaction to the outcome.
The input consists of a series of test cases. The first line contains a positive integer T (T ≤ 100), which represents the number of test cases. Each test case is represented by a line of the input, containing the choices of Sheldon and Raj, respectively, separated by a space.
我回答这个问题的代码是
T = int(input())
for i in range(T):
Sheldon, Raj = input().split(' ')
if(Sheldon == "scissors" and (Raj == "paper" or Raj == "lizard")):
Win = True
elif(Sheldon == "lizard" and (Raj == "paper" or Raj == "Spock")):
Win = True
elif(Sheldon == "Spock" and (Raj == "rock" or Raj == "scissors")):
Win = True
elif(Sheldon == "paper" and (Raj == "rock" or Raj == "Spock")):
Win = True
elif(Sheldon == "rock" and (Raj == "scissors" or Raj == "lizard")):
Win = True
elif(Raj == "scissors" and (Sheldon == "paper" or Sheldon == "lizard")):
Lose = True
elif(Raj == "lizard" and (Sheldon == "paper" or Sheldon == "Spock")):
Lose = True
elif(Raj == "Spock" and (Sheldon == "rock" or Sheldon == "scissors")):
Lose = True
elif(Raj == "paper" and (Sheldon == "rock" or Sheldon == "Spock")):
Lose = True
elif(Raj == "rock" and (Sheldon == "scissors" or Sheldon == "lizard")):
Lose = True
elif(Sheldon == Raj):
Tie = True
if(Win == True):
print("Case #{0}: Bazinga!".format(i+1))
elif(Lose == True):
print("Case #{0}: Raj cheated!".format(i+1))
elif(Tie == True):
print("Case #{0}: Again!".format(i+1))
Win = Lose = Tie = False
但我觉得时间太长了。有什么办法可以减少吗?在
试试这个,用字典
首先,祝贺你写这篇文章!你的逻辑很适合第一次尝试。在
下一步是为规则创建一个可以用相同方式查询的数据结构。一个合适的选择是
dictionary
:然后您就可以查询它,而不是重复大量的
^{pr2}$if
:相关问题 更多 >
编程相关推荐