如题,总计600个人,每次刀一个奇数位的人,请问最后谁会活下来。要看谁能够最后剩下来,可以先生成一个600人的列表,按照index删掉一个人。
import random
import pandas as pd
import matplotlib.pyplot as plt
from collections import OrderedDict
def kill():
person_list = [i for i in range(1,601)]
#print(person_list)
while len(person_list)>1:
length = len(person_list)
index = random.randint(0,length-1)
#print(index)
if index%2:
index = index-1
#print(index)
person_list.pop(index)
#print(person_list)
return person_list[0]
#print(person_list)
max_list = []
for i in range(1,100000):
value = kill()
max_list.append(value)
#print(max_list)
max_dic = {}
for item in max_list:
if item in max_dic:
max_dic[item] += 1
else:
max_dic.update({item:1})
print(max_dic)
def plot_pandas(data):
df = pd.DataFrame.from_dict(data, orient='index')
# 绘制条形图
df.plot(kind='line')
# 设置标题和轴标签
plt.title('字典值条形图')
plt.xlabel('字典键')
plt.ylabel('字典值')
# 显示图形
plt.show()
#plot_pandas(max_dic)
def plot_pandas2(data):
ordered_data = OrderedDict(sorted(data.items()))
# 创建折线图
plt.plot(ordered_data.keys(), ordered_data.values())
# 设置标题和轴标签
plt.title('字典值折线图')
plt.xlabel('字典键')
plt.ylabel('字典值')
# 显示图形
plt.show()
#print(max_dic)
plot_pandas2(max_dic)
看到图片中显示越接近600,最后留下的概率越高,实验了10万次,600号活下去的次数达到了5000