Fibonacci数列非递归解法 C++实现

#include 
using namespace std;
int fib(int n) {
	if (0 == n) {
		return 0;
	}
	if (1 == n) {
		return 1;
	}
	int f_2 = 0;
	int f_1 = 1;
	int fn = 0;
	for (int i = 2; i <= n; i++) {
		fn = f_2 + f_1;
		f_2 = f_1;
		f_1 = fn;
	}
	return fn;
}
int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	cout << fib(30) << endl;
	return 0;
}

你可能感兴趣的:(Fibonacci数列非递归解法 C++实现)