问题及代码:
/*
*copyright (t) 2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作者:郝昱猛
*完成日期:2016年3月31日
*版本号:v1.0
*问题描述:用递归函数求n个Fibnacci的值。
*程序输出:。
*/
#include<iostream>
using namespace std;
int fib(int x);
int main()
{
int n,f;
cin>>n;
f=fib(n);
cout<<f<<endl;
return 0;
}
int fib(int x)
{
if(x==1)
return 0;
else if(x==2)
return 1;
else
return fib(x-1)+fib(x-2);
}
运行结果: