题解 | 斐波那契数列
斐波那契数列
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=265&tqId=39214&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D265&difficulty=undefined&judgeStatus=undefined&tags=&title=
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ int Fibonacci(int n) { // write code here //https://blog.nowcoder.net/n/74fccc24fd324385a981bbd1bb53c01f //依据题目形式可使用递归,并且为避免递归中的重复计算,可开辟数组存储已知值 int a=1, b=1; int c=0; if(n==1||n==2){ return 1; } for(int count=3; count<=n; count++){ c=a+b; a=b; b=c; } return c; } };