AcWing 717. 简单斐波那契
原题链接
中等
作者:
Value
,
2020-07-03 09:50:27
,
所有人可见
,
阅读 725
方法一
#include <iostream>
using namespace std;
const int N = 50;
int res[N];
int main(){
int n;
cin >> n;
res[0] = 0, res[1] = 1, res[2] = 1;
for(int i = 3; i < N; i ++ ) res[i] = res[i - 1] + res[i - 2];
for(int i = 0; i < n; i ++ ){
cout << res[i];
if(i != n - 1) cout << " ";
else cout << endl;
}
return 0;
}
方法二
#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int a = 0, b = 1;
for (int i = 0; i < n; i ++ ){
cout << a << ' ';
int c = a + b;
a = b;
b = c;
}
cout << endl;
return 0;
}