用python实现:学生挑选倒垃圾人选的策略为:黑白配和石头剪刀布猜拳选出。遵循以下规则: 工、在人数多于4个人的时候,使用黑白配猜拳方式,即每人每次猜拳时出手心(白) 和手背(黑,两种方式,每次猜拳后,人数少的一方不参与下一轮猜拳。人数多的一方继续进行。 二、在猜拳人数为4人的时候,随机两两进行石头剪刀布的猜拳,如果有2个垃圾桶需要清理,则输的两个人一人一个去倒垃圾,如果只有1个垃圾桶需要清理,则第一轮输的两个人继续进行石头剪刀布的猜拳,最后输的那一个人去清理垃圾桶。 3、在猜拳人数为了人的时候,继续进行黑白配的猜拳,剩下2 人后,如果有2个垃圾桶需要清理,则两个人一人一个去倒垃圾,如果只有1个垃圾桶需要清理,则剩下两个人继续进行石头剪刀布的猜拳,最后输的那一个人去清理垃圾桶。
时间: 2024-02-21 13:59:20 浏览: 160
这里是一个用Python实现上述问题的示例代码:
```python
import random
# 定义石头剪刀布的胜负规则
def judge_gesture(gesture1, gesture2):
if gesture1 == gesture2:
return 0
elif gesture1 == 'rock' and gesture2 == 'scissors' or \
gesture1 == 'scissors' and gesture2 == 'paper' or \
gesture1 == 'paper' and gesture2 == 'rock':
return 1
else:
return -1
# 定义黑白配猜拳的胜负规则
def judge_bw_gesture(gesture1, gesture2):
if gesture1 == gesture2:
return 0
elif gesture1 == 'black': # 手背胜
return -1
else: # 手心胜
return 1
# 定义清理垃圾桶的函数
def clean_rubbish(num):
print('第%d个人去清理垃圾桶' % num)
# 定义黑白配猜拳的函数
def bw_gesture(players):
print('黑白配猜拳开始!')
black_count = 0 # 手背的人数
white_count = 0 # 手心的人数
for i in range(len(players)):
if i % 2 == 0:
gesture = 'black' # 手背
black_count += 1
else:
gesture = 'white' # 手心
white_count += 1
print('第%d个人出%s' % (i+1, gesture))
if i < len(players) - 1:
# 判断胜负,输的人不参与下一轮猜拳
j = i + 1 if judge_bw_gesture(gesture, players[i+1]) > 0 else i
players.pop(j)
if gesture == 'black':
black_count -= 1
else:
white_count -= 1
# 返回手背人数和手心人数
return black_count, white_count
# 定义石头剪刀布猜拳的函数
def play_gesture(players, num_rubbish):
print('石头剪刀布猜拳开始!')
while len(players) > 2:
win_count = [0] * len(players) # 每个人的胜利次数
for i in range(len(players)):
for j in range(i+1, len(players)):
gesture1 = random.choice(['rock', 'scissors', 'paper'])
gesture2 = random.choice(['rock', 'scissors', 'paper'])
result = judge_gesture(gesture1, gesture2)
if result > 0:
win_count[i] += 1
elif result < 0:
win_count[j] += 1
# 找到胜利次数最多的两个人
max1 = max(win_count)
idx1 = win_count.index(max1)
win_count[idx1] = -1
max2 = max(win_count)
idx2 = win_count.index(max2)
# 判断输赢
if num_rubbish == 2:
if idx1 < idx2:
clean_rubbish(idx1+1)
clean_rubbish(idx2+1)
else:
clean_rubbish(idx2+1)
clean_rubbish(idx1+1)
players.pop(idx1)
players.pop(idx2-1)
elif num_rubbish == 1:
while True:
gesture1 = random.choice(['rock', 'scissors', 'paper'])
gesture2 = random.choice(['rock', 'scissors', 'paper'])
result = judge_gesture(gesture1, gesture2)
if result != 0:
break
if result > 0:
clean_rubbish(idx1+1)
else:
clean_rubbish(idx2+1)
players.pop(idx1 if result > 0 else idx2)
players.pop(idx2-1 if result > 0 else idx1-1)
else:
print('清理垃圾桶出错!')
break
# 剩下两个人继续猜拳
gesture1 = random.choice(['rock', 'scissors', 'paper'])
gesture2 = random.choice(['rock', 'scissors', 'paper'])
result = judge_gesture(gesture1, gesture2)
if result > 0:
clean_rubbish(players.index(max(players))+1)
else:
clean_rubbish(players.index(min(players))+1)
# 主程序
def main(num_players, num_rubbish):
players = list(range(1, num_players+1))
if num_players > 4:
black_count, white_count = bw_gesture(players)
if black_count > white_count:
players = players[::2]
else:
players = players[1::2]
play_gesture(players, num_rubbish)
elif num_players == 4:
play_gesture(players, num_rubbish)
elif num_players == 3:
black_count, white_count = bw_gesture(players)
if black_count > white_count:
clean_rubbish(players[::2][0])
else:
clean_rubbish(players[1::2][0])
elif num_players == 2:
gesture1 = random.choice(['rock', 'scissors', 'paper'])
gesture2 = random.choice(['rock', 'scissors', 'paper'])
result = judge_gesture(gesture1, gesture2)
if result > 0:
clean_rubbish(1)
else:
clean_rubbish(2)
else:
print('输入人数有误!')
# 调用主程序
main(6, 2)
```
这段代码实现了学生挑选倒垃圾人选的策略。可以根据需要调整输入参数,比如说可以改变参与猜拳的人数和垃圾桶的数量。
阅读全文