牛客《剑指Offer》 -- 斐波那契数列

xiaoxiao2021-02-28  100

题目描述

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

n<=39

思路

对于n=0,应返回0。 class Solution { public: int Fibonacci(int n) { if(n==0) return 0; if(n==1||n==2) return 1; int a=1,b=1,c; n= n-2; for(int i =0;i<n;i++){ c = a + b; a = b; b = c; } return c; } };
转载请注明原文地址: https://www.6miu.com/read-83510.html

最新回复(0)