Python最接近最大输出功率的设备 /查找充电设备组合

B站影视 2025-01-05 06:47 3

摘要:from itertools import combinationsdef find_closest_power(n, powers, p_max):closest_sum = Nonemin_diff = float('inf') # 初始为无穷大# 生成所

from itertools import combinationsdef find_closest_power(n, powers, p_max):closest_sum = Nonemin_diff = float('inf') # 初始为无穷大# 生成所有子集并计算其功率和for i in range(1, n+1):for combo in combinations(powers, i):total_power = sum(combo)diff = abs(total_power - p_max)# 如果当前子集和比最接近的子集和更接近 p_max,就更新结果if diff

来源:小岳科技每日一讲

相关推荐