题解 | #Fibonacci#

Fibonacci

https://www.nowcoder.com/practice/17ad6908e36a49f4b06ea96936e8bb25

#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
#include<vector>
#include<stack>
#include<string.h>
using namespace std;

//int func(int a) {
//	if (a == 0)
//		return 0;
//	if (a == 1 || a == 2)
//		return 1;
//	else
//		return func(a - 1) + func(a - 2);
//}

int main() {
	int x;
	cin >> x;
	//int sum = func(x);
	int a = 1, b = 1, sum = 0;
	if (x == 0) {
		cout << 0 << endl;
	}
	else if (x == 1 || x == 2) {
		cout << 1 << endl;
	}
	else {
		for (int i = x;i > 2;i--) {
			sum = a + b;
			a = b;
			b = sum;
		}
		cout << sum << endl;
	}
		


}


全部评论

相关推荐

未知的命运:重新优化一下项目吧,不然你没机会了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务