1.题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). 递归实现: class Solution(): def Fibnacci(self,n): if n <= 0: return 0 if n == 1: return 1 return self.Fibnacci(n-1) + self.Fibnacci(n-2) 非递归实现: def Fibnacci(n): result = [0,1] if n <= 1: return
定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 阶乘实例 n = int(input(">>:")) def f(n): s = 1 for i in range(2, (n + 1)): s *= i return s print(f(n)) 递归 def factorial_new(n): if n==1: return 1 return n*factorial_new(n-1) print(factorial_new(3))
# coding=gbk # 迭代法---1 def fibonacci (n): if n == 0 or n == 1: return n else : a = 0 b = 1 for i in range (n-1) : t = a a = b b = a + t return b number = eval (input ("请输入您要计算的斐波那契数列的项\n")) cc= fibonacci (number) print (cc) # 迭代法---2 def fibonac
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()
Fibonacci Sequence # fibonacci sequence 斐波那契数列 def fibonacci_for(n): # 使用for循环返回n位斐波那契数列列表 li = [] for i in range(n+1): if i == 0 or i == 1: li.append(1) else: li.append(li[i-2] + li[i-1]) return li def fibonacci_sequence(over, x=1, y=1): # 返回一个over值