TheAlgorithms-Python

Форк
0
65 строк · 1.4 Кб
1
"""
2
Coin sums
3
Problem 31: https://projecteuler.net/problem=31
4

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

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

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

15

16
def one_pence() -> int:
17
    return 1
18

19

20
def two_pence(x: int) -> int:
21
    return 0 if x < 0 else two_pence(x - 2) + one_pence()
22

23

24
def five_pence(x: int) -> int:
25
    return 0 if x < 0 else five_pence(x - 5) + two_pence(x)
26

27

28
def ten_pence(x: int) -> int:
29
    return 0 if x < 0 else ten_pence(x - 10) + five_pence(x)
30

31

32
def twenty_pence(x: int) -> int:
33
    return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x)
34

35

36
def fifty_pence(x: int) -> int:
37
    return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x)
38

39

40
def one_pound(x: int) -> int:
41
    return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x)
42

43

44
def two_pound(x: int) -> int:
45
    return 0 if x < 0 else two_pound(x - 200) + one_pound(x)
46

47

48
def solution(n: int = 200) -> int:
49
    """Returns the number of different ways can n pence be made using any number of
50
    coins?
51

52
    >>> solution(500)
53
    6295434
54
    >>> solution(200)
55
    73682
56
    >>> solution(50)
57
    451
58
    >>> solution(10)
59
    11
60
    """
61
    return two_pound(n)
62

63

64
if __name__ == "__main__":
65
    print(solution(int(input().strip())))
66

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

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

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

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