LJC13618059782 @ 2023-10-30 21:38:24
#include<bits/stdc++.h>
using namespace std;
long long a[40];//全局变量自动初始化为0
int f(int x)//f函数的作用走到第x级台阶的走法总数。
{
if(x==1)return 1;
if(x==2)return 2;
if(a[x-1]==-1)a[x-1]=f(x-1);
if(a[x-2]==-1)a[x-2]=f(x-2);
return a[x-1]+a[x-2];
}
int main()
{
long long n,ans;
while(cin>>n)
{
for(int i=1;i<=n;++i)a[i]=-1;//将a数组初始化为-1
ans=f(n);
cout<<ans<<endl;
}
return 0;
}
//只有50分,大佬求解
by Night_fall @ 2023-10-30 21:40:30
要用高精度,单纯递推位数不够
by Syncc @ 2023-10-30 21:41:31
需要高精啊,不过用 unsigned long long 能60分
by LJC13618059782 @ 2023-10-30 21:45:38
@Razer_System 知道了 谢谢
by LJC13618059782 @ 2023-10-30 21:46:22
@Night_fall 知道了 谢谢
by LJC13618059782 @ 2023-10-30 21:57:02
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> add(vector<int> &A, vector<int> &B)
{
if (A.size() < B.size()) return add(B, A);
vector<int> C;
int t = 0;
for (int i = 0; i < A.size(); i ++ )
{
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
int main(){
while(cin>>n){
vector<int> a={1},b={2},c;
if(n==1){
cout<<1<<endl;
}
if(n==2){
cout<<2<<endl;
}
if(n>=3){
for(int i=3;i<=n;i++){
c=add(a,b);
a=b;
b=c;
}
for (int i = c.size() - 1; i >= 0; i--)
cout<<c[i];
cout<<endl;
}
}
return 0;
}
//谢谢,我知道了