当前位置:网站首页>实例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)
边栏推荐
- Let's briefly talk about the chips commonly used in mobile phones - OVP chips
- Anonymous structure in C language
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
- [tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
- Imx6ull bare metal development learning 1-assembly lit LED
- Tailq of linked list
- 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
- 1-stm32 operation environment construction
- Google sitemap files for rails Projects - Google sitemap files for rails projects
猜你喜欢
Create inf module in AMI code
Installation and use of libjpeg and ligpng
List of linked lists
Wifi-802.11 negotiation rate table
Charge pump boost principle - this article will give you a simple understanding
Take you to understand the working principle of lithium battery protection board
After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
Improve lighting C program
MySQL之MHA高可用集群
Introduction of air gap, etc
随机推荐
Detailed explanation of SQL server stored procedures
Verilog -- state machine coding method
UEFI development learning 6 - creation of protocol
Semiconductor devices (I) PN junction
Solutions to compilation warnings in Quartus II
Some tips for using source insight (solve the problem of selecting all)
Problem solving: interpreter error: no file or directory
Fundamentals of C language
Programming knowledge -- basis of C language
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
My-basic application 1: introduction to my-basic parser
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Classic application of MOS transistor circuit design (2) - switch circuit design
Explain task scheduling based on Cortex-M3 in detail (Part 1)
Arduino uses nrf24l01+ communication
go依赖注入--google开源库wire
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
Relationship between line voltage and phase voltage, line current and phase current
STM32 tutorial triple ADC interleaved sampling