#打印斐波那契数列 f0 = 0 f1 = 1 for n in range(2,101): fn = f1 + f0 if fn <= 100: print(fn) f0 = f1 f1 = fn 方法2: #打印斐波那契数列,100以内 print(0) print(1) a = 0 b = 1 while True: c = a+b if c > 100: break a = b b = c print(c)
#打印斐波那契数列的第101项 a = 1 b = 1 for count in range(99): a,b = b,a+b else: print(b) 方法2: #打印斐波那契数列的第101项 a = 1 b = 1 for i in range(2,101): if i == 100: print(a+b) b += a a = b-a
著名的斐波拉契数列(Fibonacci),除第一个和第二个数外,任意一个数都可由前两个数相加得到: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 如果用Python的列表生成式,很难写出来 如果用函数和生成器的话就很容易了 def fib(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b n = n + 1 return 'done'需要注意上面的 a,b = b, a+b 相当于: t = (b
day4 --------------------------------------------------------------- 实例006:斐波那契数列 题目 斐波那契数列. 题目没说清楚,大概说的是输出制定长度的数列吧,想了想实现如下: 1 a = int(input("请输入斐波那契数列位数:")) 2 list = [] 3 for i in range(a): 4 if i <2: 5 list.append(i) 6 else: 7 list.append(l
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 斐波那契数列求和 { class Program { static void Main(string[] args) { Console.WriteLine()); Console.WriteLine()); Console.WriteLine()