编辑代码

#include <iostream>
using namespace std;
int fib(int n)
{
   if(n<1)
   return 0;
if(n==1||n==2)
return 1;
return fib(n-1)+fib(n-2);
}
int fib_loop(int n){
	if(n<1)
	return 0;
	int first=1;
	int second=1;
	int ret=first;
	while(n>2)
	{
		ret=first+second;
		first=second;
        second=ret;
		--n;
	}
	return ret;
}
int fib_optimized(int n,int first,int second)
{
	if(n<1)
	return 0;
	if(n==1)
	return first;
    else if(n==2)
	return second;
	else
	return fib_optimized(n-1,second,first+second);
}

int main() {
	int n;
	cin>>n;
	cout << fib(n)<<endl;
	return 0;
}