当前位置:网站首页>实例006:斐波那契数列
实例006:斐波那契数列
2022-07-05 08:16:00 【懒笑翻】
实例006:斐波那契数列
题目:斐波那契数列。输出给定个数的斐波那契数列。
#题目分析:斐波那契数列(Fibonacci sequence),从1,1开始,后面每一项等于前面两项之和。
#程序分析:我们可以先定义一个列表用来存放斐波那契数列,根据斐波那契数列的规律,从第3项开始,每一项是前两项的和,获取到前两项的和后存入数列中。
# 当最开始进入 i=0的时候,a=1,b=a+b=2, 当i=1时,a=2,b=a+b=3,当i=2时,a=3,b=a+b=5,·······
list_fib = [1] # 定义一个列表用来存放斐波那里数列,初始化第一个元素值为1
target = int(input("输入你想要前几项的裴波那契数列"))
res = 0
a, b = 1, 1
for i in range(target - 1):
a, b = b, a + b
list_fib.append(a)
print(list_fib)
把上面一段加入到循环中我们可以测试多次看效果:
for j in range(5):
list_fib = [1] # 定义一个列表用来存放斐波那里数列,初始化第一个元素值为1
target = int(input("输入你想要前几项的裴波那契数列"))
res = 0
a, b = 1, 1
for i in range(target - 1):
a, b = b, a + b
list_fib.append(a)
print(list_fib)
边栏推荐
- Vofa+ software usage record
- Detailed summary of FIO test hard disk performance parameters and examples (with source code)
- Synchronization of QT multithreading
- Wifi-802.11 negotiation rate table
- MHA High available Cluster for MySQL
- Detailed explanation of SQL server stored procedures
- Network port usage
- STM32 single chip microcomputer - external interrupt
- C, Numerical Recipes in C, solution of linear algebraic equations, LU decomposition source program
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
猜你喜欢
Wifi-802.11 negotiation rate table
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Network communication process
Process communication mode between different hosts -- socket
Consul安装
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Tailq of linked list
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Consul installation
Improve lighting C program
随机推荐
Step motor generates S-curve upper computer
亿学学堂给的证券账户安不安全?哪里可以开户
Stablq of linked list
Interview catalogue
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Why is 1900 not a leap year
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Introduction of air gap, etc
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Programming knowledge -- assembly knowledge
C WinForm [view status bar -- statusstrip] - Practice 2
Tailq of linked list
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
动力电池UL2580测试项目包括哪些
Some tips for using source insight (solve the problem of selecting all)
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Void* C is a carrier for realizing polymorphism
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
STM32 single chip microcomputer - external interrupt
Management and use of DokuWiki (supplementary)