zoukankan      html  css  js  c++  java
  • 20190121-n个人围成一圈,凡报到3的人退出圈子,最后留下的是原来第几号的那位

    1. 报数问题:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位


    思路:此题主要问题在于但凡报到3的人退出圈子,而报数的号码与圈子的顺序的关系是需要循环的一直报1,2,3,1,2,3,1,2,3。。。当最后一个人报完数的时候,第一个人需要接着最后的人的数报,因此报数的数字与圈子的序列是2个独立的计数方式

    考虑使用递归函数,当s中的数字剩余大于1的时候,需要进行报数检查,当s中数字剩余1个的时候,返回s

    def iter_func(s,i=1):
        result=[]
        for j in s:
            if (i==1 and len(s)>1) or (i==2 and len(s)>1):
                result.append(j)
                i+=1
            elif i ==3 and len(s)>1:
                i=1
                continue
            elif len(s)==1:
                break
    #运算主体,当s中不止一个数字的时候,将s中不是报到3的数字插入一新的列表中
        if len(s)==1:
            return s
        else:
            return iter_func(result,i)
    s = list(range(1,11))
    print(s)
    print(iter_func(s))
    s1 = list(range(1,21))
    print(s1)
    print(iter_func(s1))

     另外一个写法

    def iter_func(s,i=1):
        result=[]
        if len(s)>1:
            for j in s:
                if i==1 or i==2:
                    result.append(j)
                    i+=1
                    print(result)
                elif i ==3:
                    i=1
                    continue
    #递归主体思路
        if len(s)==1:
            return s
    #递归结束条件
        else:
            return iter_func(result,i)
  • 相关阅读:
    [Panzura] identify user operations(copy, open, read ... ) in audit log
    Spark 学习
    Zeppelin 学习
    Delta Lake 学习
    传染病模型 SI
    xcodebuild和xcrun实现自动打包iOS应用程序
    控制UIlabel 垂直方向对齐方式的 方法
    ALAssetsLibrary
    CATransform3D
    AVFoundation的使用
  • 原文地址:https://www.cnblogs.com/hyj691001/p/10296950.html
Copyright © 2011-2022 走看看