9月1日 拼多多代码

奇偶数

比较简单,按照题意作就好了python

if __name__ == "__main__":
    data, n = input().strip().split(";")
    n = int(n)
    data = list(map(int, data.split(",")))
    odata = [i for i in data if i%2==0]
    jdata = [i for i in data if i%2!=0]

    odata = sorted(odata, reverse = True)
    jdata = sorted(jdata, reverse = True)
     
    res = []
    if n<=len(odata):
        res = odata[:n]
    else:
        res = odata + jdata[:n-len(odata)]
    
    res = [str(i) for i in res]
    print(','.join(res))

复制代码

掷骰子

if __name__ == "__main__":
    n = int(input().strip())
    data = list(map(int, input().strip().split(" ")))

    from functools import reduce
    all = reduce(lambda x,y:x * y, data) # 计算列表元素乘积

    data = sorted(data, reverse = True) #data 递减排序
    
    temp_dict = {} #保存最大值的状况数目

    while 0 not in data: #当data中元素均不为0的时候
        max_d = data[0]  # 最大值为第一个
        temp = reduce(lambda x,y:x * y, data[1:])
        if max_d in temp_dict:
            temp_dict[max_d] = temp_dict[max_d] + temp # 判断key是否在字典中,在的话结果相加
        else:
            temp_dict.update({max_d:temp}) #不然添加结果
        data[0] = data[0]-1 #最大值减一
        data = sorted(data, reverse = True) # 对data从新排序
    
    res = 0
    for k,v in temp_dict.items():
        res = res + k*v/all  #计算几率
    
    print ('%.2f' %(res))
复制代码

Kth

先是暴力作的,而后内存超出了,最后两分钟想到用最小堆,没有提交,自测没有问题app

from heapq import heappushpop, heappop

if __name__ == "__main__":
    n,m,k = list(map(int, input().strip().split(" ")))
    
    heap = [i for i in range(k)]

    for i in range(n):
        for j in range(m):
            heappushpop(heap,(i+1)*(j+1))  #更新最小堆

    print(heappop(heap)) #输出第K大的数
复制代码
相关文章
相关标签/搜索