To divide Python integers N by the number of cases received

Asked 2 years ago, Updated 2 years ago, 13 views

Integer N

For example, when 3 is entered,

The number of methods that can only be expressed as the sum of 3 with 1 and 2

(1,1,1), (2,1), (1,2) A total of 3

How do I solve this? I have no idea.

python

2022-09-20 16:36

1 Answers

I don't know how to divide by 1 and 2, but the method of dividing by n or less is as follows.

def fib(n):
    if n == 0:
        return 0
    elif n == 1 or n == 2:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)
n = int(input())
print(fib(n+1))
arr = [1, 1]
n = int(input())
for i in range(2, n+1):
    arr.append(0)
    arr[i] = arr[i-2] + arr[i-1]
print(arr[n])


2022-09-20 16:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.