# 功能:求斐波那契数列第 n 个数的值
# 在此设置 n n = 30 print(‘\n‘);print(‘n = ‘,n)
# 代码生成 Fibonacci 序列,存于数组A A = [0]*n A[0] = 1;A[1] = 1 for i in range(2,n): A[i] = A[i-1] + A[i-2] print(‘\n前 n 个数的斐波那契数列为:‘);print(A) print(‘\n斐波那契数列第 n 个数的值为:‘,A[n-1]); print(‘\n‘)
# 运行结果
n = 30 前 n 个数的斐波那契数列为: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040] 斐波那契数列第 n 个数的值为: 832040
原文地址:https://www.cnblogs.com/aiyou-3344520/p/12128031.html
时间: 2024-11-10 05:20:03