输入包括一个整数N,(1<=N<90)。
可能有多组测试数据,对于每组数据, 输出当楼梯阶数是N时的上楼方式个数。
4
5
def fun(n): n1 = 1 n2 = 2 if n == 1: return 1 elif n == 2: return 2 count = 0 for i in range(3,n+1): count = n1+n2 n1 = n2 n2 = count return count if __name__ == '__main__': a = int(input()) print(fun(a))
while True: try: n=int(input().strip()) dp=[0]*n if n==1 or n==2: print(n) else: dp[0]=1 dp[1]=2 for i in range(2,n): dp[i]=dp[i-1]+dp[i-2] print(dp[-1]) except: break
from functools import reduce try: while 1: print(reduce(lambda a, x: a + [a[-1]+a[-2]], range(95), [1, 2])[int(input()) - 1]) except: pass
while True: try: a,arr=int(input()),[1,2] while len(arr)<a: arr.append(arr[-1]+arr[-2]) print(arr[a-1]) except: break
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题