一、序
场景:公司报销需要用打车发票,金额不能多于报销额度,自己搭配出最接近报销的金额
二、实现思路
读取全部打车能报销的金额,全部相加,留下小等于报销金额的组合,然后取最大值与组合
三、实现代码
实现代码
import itertools import pandas as pd import numpy as npnum_data=pd.read_excel('F:\\file\\work_skill\\报销\\num_list.xlsx',header=0)
num_data1 = np.array(num_data)
num_x_list = num_data1.tolist()
num_result = sum(num_x_list, [])
def get_result(hope, list1):
result = [] # 结果列表
for i in range(1, len(list1) + 1):
iter = itertools.combinations(list1, i) # 求每个长度的组合
group_item = list(iter)
for x in range(0, len(group_item)):
if sum(group_item[x]) <= hope:
result.append(group_item[x])
results=[]
for i in range(0,len(result)):
item_sum = 0
for j in range(0,len(result[i])):
item_sum+=result[i][j]
results.append(item_sum)
max_index=results.index(max(results))
print("最接近的值为:",round(results[max_index],2),"组合是:",result[max_index])
if __name__ == '__main__':
money = int(input("请输入您的报销金额:"))
get_result(money, num_result)