python_凑数

python_凑数

假设数量是x,y,z,且只能是整数,单价分别是4,5,6,目标是10,如果4X+5y+6z=10,求想,x,y,z分别是多少

def find_solutions(prices, target, index=0, current_sum=0, current_solution=[]):
    if current_sum == target and index == len(prices):
        yield tuple(current_solution)
    elif index < len(prices):
        for value in range(0, 3):  # 变量取值范围
            new_sum = current_sum + (prices[index] * value)
            if new_sum <= target:
                yield from find_solutions(prices, target, index + 1, new_sum, current_solution + [value])

def solve_combination(prices, target):
    solutions = list(find_solutions(prices, target))
    return solutions if solutions else "No solution"

# 定义单价和目标金额
prices = [4,5,6]
target = 10

# 调用函数并打印结果
result = solve_combination(prices, target)
print(result)

结果:
[(0, 2, 0), (1, 0, 1)]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值