TheAlgorithms-Python

Форк
0
59 строк · 1.5 Кб
1
"""
2
Problem 31: https://projecteuler.net/problem=31
3

4
Coin sums
5

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

9
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
10
It is possible to make £2 in the following way:
11

12
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
13
How many different ways can £2 be made using any number of coins?
14

15
Hint:
16
    > There are 100 pence in a pound (£1 = 100p)
17
    > There are coins(in pence) are available: 1, 2, 5, 10, 20, 50, 100 and 200.
18
    > how many different ways you can combine these values to create 200 pence.
19

20
Example:
21
    to make 6p there are 5 ways
22
      1,1,1,1,1,1
23
      1,1,1,1,2
24
      1,1,2,2
25
      2,2,2
26
      1,5
27
    to make 5p there are 4 ways
28
      1,1,1,1,1
29
      1,1,1,2
30
      1,2,2
31
      5
32
"""
33

34

35
def solution(pence: int = 200) -> int:
36
    """Returns the number of different ways to make X pence using any number of coins.
37
    The solution is based on dynamic programming paradigm in a bottom-up fashion.
38

39
    >>> solution(500)
40
    6295434
41
    >>> solution(200)
42
    73682
43
    >>> solution(50)
44
    451
45
    >>> solution(10)
46
    11
47
    """
48
    coins = [1, 2, 5, 10, 20, 50, 100, 200]
49
    number_of_ways = [0] * (pence + 1)
50
    number_of_ways[0] = 1  # base case: 1 way to make 0 pence
51

52
    for coin in coins:
53
        for i in range(coin, pence + 1, 1):
54
            number_of_ways[i] += number_of_ways[i - coin]
55
    return number_of_ways[pence]
56

57

58
if __name__ == "__main__":
59
    assert solution(200) == 73682
60

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.