当前位置:网站首页>实例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)
边栏推荐
- STM32 single chip microcomputer -- volatile keyword
- Arduino uses nrf24l01+ communication
- C WinForm [realize the previous and next selection pictures] - practice 7
- C WinForm [display real-time time in the status bar] - practical exercise 1
- UEFI development learning 4 - getting to know variable services
- Semiconductor devices (III) FET
- Ble encryption details
- Halcon's practice based on shape template matching [2]
- Circleq of linked list
- Basic information commands and functions of kernel development
猜你喜欢

FIO测试硬盘性能参数和实例详细总结(附源码)

Negative pressure generation of buck-boost circuit
![C WinForm [help interface - send email] - practice five](/img/2a/c4e7abe054e6fdd45acc7d297a033d.jpg)
C WinForm [help interface - send email] - practice five

PMSM dead time compensation

Explain task scheduling based on Cortex-M3 in detail (Part 2)
![[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication](/img/05/0f63e4cd3da24e5b956ec5899b939d.jpg)
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication

MySQL之MHA高可用集群

Management and use of DokuWiki

STM32 single chip microcomputer - bit band operation

Shell script basic syntax
随机推荐
Basic information commands and functions of kernel development
Basic embedded concepts
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Problem solving: interpreter error: no file or directory
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Talk about the circuit use of TVs tube
动力电池UL2580测试项目包括哪些
Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
Volatile of C language
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
Charge pump boost principle - this article will give you a simple understanding
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
Management and use of DokuWiki (supplementary)
Keil use details -- magic wand
DCDC circuit - function of bootstrap capacitor
Reasons for rapid wear of conductive slip rings
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Void* C is a carrier for realizing polymorphism
Some thoughts on extracting perspectives from ealfa and Ebeta