Project Euler Problem 31

Problem 31

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).

It is possible to make £2 in the following way:

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p

How many different ways can £2 be made using any number of coins?
#  有1p,2p,5p,10p,20p,50p,100p,200p 八种硬币,要得到200p,有多少种取法?(硬币个数不限)

思路:对于每一种取法,分为多次取出,对于每一次取出,都取余下硬币种最大的
比如:一类取法:第一次取5p,则剩余195p待取,第二次可以的取法有5p,2p,1p;
                           如果第二次取5p,第三次可以取5p,2p,1p,如果第二次取2p,第三次可以取2p,1p,...
                            ...... 

coins_list = [1,2,5,10,20,50,100,200]
total = 200
# 每次取余下中最大的一枚硬币

def ways(total,coins_list):
    if coins_list == [1]:
        return 1
    sum1 = 0
    for i in range(len(coins_list)):
        reminder = total - coins_list[i]
        if reminder == 0:
            sum1 += 1
        elif reminder > 0:
            sum1 += ways(reminder,coins_list[:i+1])
    return sum1


print(ways(total,coins_list))
结果:73682
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值