比較簡單,按照題意作就好了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))
複製代碼
先是暴力作的,而後內存超出了,最後兩分鐘想到用最小堆,沒有提交,自測沒有問題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大的數
複製代碼