使用c++编程语言,求第n个斐波那契数,代码如下

#include
using namespace std;

int fib_2(int n)
{
	if (n <= 1)
	{
		return n;
	}

	int first = 0;
	int second = 1;
	for (int i = 0; i < n - 1; i++)
	{
		int sum = first + second;
		first = second;
		second = sum;
	}

	return second;
}

int main()
{
	cout << fib_2(8)<

你可能感兴趣的:(c++,算法,开发语言)