求斐波那契数列项

#include <iostream>
#include <cstdio>
using namespace std;
int fib(int n)
{
  // 递归终止,直接返回
  if(n == 1 || n == 2)
    return 1;
  // 两次递归调用,求和并返回
  return fib(n - 1) + fib(n - 2);
}
int main()
{
  int n;
  cin>>n;
  cout<<fib(n);
  return 0;
}

posted @ 2020-03-28 12:36  py佐料  阅读(122)  评论(0编辑  收藏  举报