V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
t895
V2EX  ›  Python

Python 求数

  •  
  •   t895 · 2018-09-17 22:51:00 +08:00 · 1266 次点击
    这是一个创建于 2019 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看了李永乐老师的问题,100 个囚犯的,看到有人用 java 实现了,想问下用 python 可以实现吗? 附 java

    public static void main(String[] args) { Map<Integer, Integer> people100 = new HashMap<>(); for (int i = 1; i < 101; i++) { people100.put(i, i); }

        recursive(people100);
    }
    
    public static void recursive(Map<Integer, Integer> map) {
        Integer counter = 1;
        Map<Integer, Integer> resultMap = new HashMap<>();
        if (map.size() == 1) {
            Set<Integer> integers = map.keySet();
            Iterator<Integer> iterator = integers.iterator();
            for (Integer integer : integers) {
                System.out.println("最终活着的人的编号是: "+map.get(integer));
                return;
            }
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getKey() % 2 == 0) {
                resultMap.put(counter++, entry.getValue());
            }
        }
        recursive(resultMap);
    }
    
    3 条回复    2018-09-18 10:46:26 +08:00
    Raisu
        1
    Raisu  
       2018-09-17 23:33:04 +08:00
    。。。不如把问题贴出来。。。。
    PythonAnswer
        2
    PythonAnswer  
       2018-09-17 23:43:42 +08:00 via iPhone
    哈哈 我也看他视频
    xpresslink
        3
    xpresslink  
       2018-09-18 10:46:26 +08:00
    >>> def recur(p_list):
    ◇ ◇ ◇ ◇if len(p_list)==1:
    ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇print('last is number:', p_list[0])
    ◇ ◇ ◇ ◇else:
    ◇ ◇ ◇ ◇ ◇ ◇ ◇ ◇recur(p_list[1::2])

    >>> prisoners = list(range(1,101))
    >>> recur(prisoners)
    last is number: 64
    >>>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5886 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 06:17 · PVG 14:17 · LAX 23:17 · JFK 02:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.