• 问题求解:总计600人,每次刀一个奇数位的人,最后剩下谁的概率最高 暴力求解法


    1.问题描述:

    如题,总计600个人,每次刀一个奇数位的人,请问最后谁会活下来。要看谁能够最后剩下来,可以先生成一个600人的列表,按照index删掉一个人。

    2.代码实现

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69

    3结果

    在这里插入图片描述
    看到图片中显示越接近600,最后留下的概率越高,实验了10万次,600号活下去的次数达到了5000

  • 相关阅读:
    SVG 基本语法
    实战经验分享FastAPI 是什么
    AWS Lambda – 函数版本,别名,API网关,CodeDeploy协同
    受了刺激,决定专升本
    通过usb串口发送接收数据
    Java程序之让气球上升
    R 语言降维的 PCA 与自动编码器
    linux信号
    风向对风力机发电的影响
    第2章-矩阵及其运算-矩阵创建(1)
  • 原文地址:https://blog.csdn.net/LCY133/article/details/133903188