斐波那契数列python实现写法

下面是自己写着玩的:
a=1
b=1
temp=int(input(“输入第几个数:”))
if temp == 1:
print(“第”,temp,“个数是”,0)
elif temp2 or temp3:
print(“第”,temp,“个数是”,1)
else:
for i in range(temp-3):
sum=a+b
a=b
b=sum
print(“输出第”,temp,“个数是:”,b)

总结这里的核心思想是怎么,怎么把后面一个数赋值给前面的一个数,用到一个介质(sum作为桥梁),不断赋值传递。编程思想,就是抽象的过程,抽象表达需要介质。

================================================================================菜鸟网站上的例子

-- coding: UTF-8 --

Filename : test.py

author by : www.runoob.com

Python 斐波那契数列实现

获取用户输入数据

nterms = int(input(“你需要几项?”))

第一和第二项

n1 = 0
n2 = 1
count = 2

判断输入的值是否合法

if nterms <= 0:
print(“请输入一个正整数。”)
elif nterms == 1:
print(“斐波那契数列:”)
print(n1)
else:
print(“斐波那契数列:”)
print(n1,",",n2,end=" , “)
while count < nterms:
nth = n1 + n2
print(nth,end=” , ")
# 更新值
n1 = n2
n2 = nth
count += 1

执行以上代码输出结果为:

你需要几项? 10
斐波那契数列:
0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

下面是网友的总结:
https://www.cnblogs.com/areyouready/p/8979973.html

https://www.cnblogs.com/panlq/p/9307203.html

https://www.runoob.com/python3/python3-fibonacci-sequence.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值