当前位置:网站首页>实例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)
边栏推荐
- [trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
- Negative pressure generation of buck-boost circuit
- Hardware and software solution of FPGA key chattering elimination
- Several important parameters of LDO circuit design and type selection
- Network communication process
- Drive LED -- GPIO control
- 动力电池UL2580测试项目包括哪些
- Take you to understand the working principle of lithium battery protection board
- My-basic application 1: introduction to my-basic parser
- Charge pump boost principle - this article will give you a simple understanding
猜你喜欢

Factors affecting the quality of slip rings in production

Summary -st2.0 Hall angle estimation

Classic application of MOS transistor circuit design (1) -iic bidirectional level shift

Consul installation

Keil use details -- magic wand

Step motor generates S-curve upper computer

Array integration initialization (C language)

1-stm32 operation environment construction

Installation and use of libjpeg and ligpng

VESC Benjamin test motor parameters
随机推荐
Interview catalogue
STM32 --- NVIC interrupt
How to copy formatted notepad++ text?
Basic embedded concepts
Hardware and software solution of FPGA key chattering elimination
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
Tailq of linked list
Naming rules for FreeRTOS
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
Embedded composition and route
Network communication model -- Network OSI tcp/ip layering
UEFI development learning 3 - create UEFI program
Sizeof (function name) =?
C WinForm [help interface - send email] - practice five
Process communication mode between different hosts -- socket
Buildroot system for making raspberry pie cm3
The firmware of the connected j-link does not support the following memory access
C WinForm [change the position of the form after running] - Practical Exercise 4
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc