码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 问题求解:总计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

  • 相关阅读:
    QT UI设计笔记
    多商户商城系统功能拆解38讲-平台端营销-砍价商品
    基于transformer的心脑血管心脏病疾病预测
    Linux----生产者和消费者模型
    netty系列之:EventExecutor,EventExecutorGroup和netty中的实现
    通过内网穿透实现文件共享,Python—行代码轻松实现公网访问
    基于Java的私人牙医诊所(医院)系统
    java中的instanceof 的用法
    Java进阶03 IO基础
    [车联网安全自学篇] 四十八. Android安全之聊聊Drozer模块插件
  • 原文地址:https://blog.csdn.net/LCY133/article/details/133903188
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号