题目描述
blablabla
样例
blablabla
算法1
(fib(爬楼梯法)) $O(n)$
C++ 代码(自己的)
#include <iostream>
using namespace std;
int main(){
int n;
cin>>n;
int last = 0,now = 1;
if(n == 1)
cout<<last<<endl;
else if(n == 2)
cout<<last<<' '<<now<<endl;
else
{
cout<<last<<' '<<now<<' ';
int fib = 0;
while(n-- > 2)
{
fib = now + last;
last = now;
now = fib;
cout<<fib<<' ';
}
}
return 0;
}
算法2
(同上) $O(n)$
C++ 代码(y总的)
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a = 0, b = 1;
int n;
cin >> n;
for (int i = 0; i < n; i ++ )
{
cout << a << ' ';
int c = a + b;
a = b, b = c;
}
cout << endl;
return 0;
}