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

猜你喜欢

转载自blog.csdn.net/wxinbeings/article/details/80294282