• python面试题


    给定数组[0-9] 和 boll_array[0111011110], 0表示可以输出,
    也可以不输出,1必须输出对应位,输出所有可能情况(按字符串升序)
    import copy
    a = [0,1,2,3,4,5,6,67,8,9] # 数字数组
    b = [0,1,1,1,0,1,1,1,1,0] # 对应的布尔值数组
    # b = list(map(int,input(">>>").strip().split()))
    index = []
    for i in range(0,len(b)):
        if b[i] == 1:
            b[i] = str(i) # 将布尔值为1的位置赋予对应的数值
        if b[i] == 0:
            index.append(i) # 记录布尔值为0的位置索引
            b[i] = '' # 同时赋予空
    # print(b)
    res = [b]
    # print(res)
    for ind in index:
        # count=0
        for i in range(len(res)):#第一次循环一次,第二次循环二次,第三次循环四次
            # count  += 1
            # print(res[i])
            c = copy.copy(res[i])
            c[ind] = str(ind) # 对结果列表中的每个组合依次添加布尔值为0的位置所对应的数字
            # print(c)
            res.append(c)
    print(res)
        # print(count)
    for i in range(len(res)):
        res[i] = ''.join(res[i])
    res.sort() # 排序
    for i in res:
        print(i)

    第一层循环 ind=0时 此时res中存在时着原始列表['', '1', '2', '3', '', '5', '6', '7', '8', ''],c拷贝一份并将0赋给c[0],然后将c加到res中

    此时res=[['', '1', '2', '3', '', '5', '6', '7', '8', ''],['0', '1', '2', '3', '', '5', '6', '7', '8', '']] ind=0循环完毕

    ind=4时,第二层循环循环二次,c分别拷贝了res并将4赋给c[4],然后依次加入到了res中,此时

    res=

    [['', '1', '2', '3', '', '5', '6', '7', '8', ''],
     ['0', '1', '2', '3', '', '5', '6', '7', '8', ''], 
    ['', '1', '2', '3', '4', '5', '6', '7', '8', ''],
     ['0', '1', '2', '3', '4', '5', '6', '7', '8', '']]

    ind=9时,第二层循环循环四次,c分别拷贝了res并将9赋给c[9],然后依次加入到了res中

    最终
    列表输出结果
    [['', '1', '2', '3', '', '5', '6', '7', '8', ''],
     ['0', '1', '2', '3', '', '5', '6', '7', '8', ''], 
    ['', '1', '2', '3', '4', '5', '6', '7', '8', ''],
     ['0', '1', '2', '3', '4', '5', '6', '7', '8', ''], 
    ['', '1', '2', '3', '', '5', '6', '7', '8', '9'],
     ['0', '1', '2', '3', '', '5', '6', '7', '8', '9'],
     ['', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
     ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']]
    
    Process finished with exit code 0
    有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
    import copy
    lst=[]
    for a in range(1,5):
        for b in range(1, 5):
            for c in range(1, 5):
                d=str(a)+str(b)+str(c)
                lst.append(d)
    lst0=copy.deepcopy(lst)
    for i in lst0:
        if i[0]==i[1] or i[1]==i[2] or i[0]==i[2]:
            lst.remove(i)
    print(len(lst))
    print(lst)
    一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
    找出1000以内的所有完数。
    def func(n):
        a=n/2
        # print(a)
        lst=[]
        count=1
        while count<a+1:
            if n%count==0:
                # print(n,count)
                # n=n/count
                lst.append(count)
            count+=1
            # print(count)
            #     count=2
            # else:
            #     count += 1
        return lst
    # print(func(28))
    for i in range(2,10000):
        if sum(func(i))==i:
            print(i)
    将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
    n=int(input(">>>"))
    b=n
    a=n/2
    # print(a)
    lst=[]
    count=2
    while count<a+1:
        if n%count==0:
            # print(n,count)
            n=n/count
            lst.append(count)
        # print(count)
        else:
            count += 1
    print(f"{b}=",end="")
    for i in range(len(lst)):
        if i == len(lst)-1:
            print(lst[i])
        else:
            print(f"{lst[i]}*",end="")
    # 输入包括字符串s,s的长度length(1 ≤ length ≤ 50),字符串中只包含'0'和'1'
    # 输出一个整数,表示最长的满足要求的子串长度。
    msg="11111111000011110"
    count=0
    lst=[]
    if len(msg)==1:
        print(1)
    else:
        for i in range(len(msg)-1):
            if msg[i+1]==msg[i]:
                count += 1
                lst.append(count)
            else:
                count=0
        if lst==[]:
            print(1)
        else:
            print(max(lst)+1)




























  • 相关阅读:
    hdu_5791_Two(DP)
    hdu_5783_Divide the Sequence(贪心)
    hdu_5769_Substring(后缀数组)
    hdu_5778_abs(暴力)
    hdu_5776_sum(前缀和维护)
    hdu_5777_domino(贪心)
    [wikioi2069]油画(贪心)
    [bzoj 1503][NOI 2004]郁闷的出纳员(平衡树)
    数据结构练习
    [poj3274]排排站(Hash)
  • 原文地址:https://www.cnblogs.com/ellisonzhang/p/10280150.html
Copyright © 2020-2023  润新知