走楼梯的记忆化和递推
天赋能让人闪闪发光的东西,努力也可以
include <bits/stdc++.h>
using namespace std;
int dp[50];
int f(int x)
{
if(dp[x]!=-1) return dp[x];
return dp[x]=f(x-1)+f(x-2);
}
int main()
{
int t;cin>>t;
while(t--)
{
memset(dp,-1,sizeof(dp));
dp[1]=0;dp[2]=1;dp[3]=2;
int n;cin>>n;
cout<<f[n]<<endl;
}
}
include <bits/stdc++.h>
using namespace std;
int dp[50];
int main()
{
int t;cin>>t;
while(t--)
{
int n;cin>>n;
memset(dp,0,sizeof(dp));
dp[1]=0;dp[2]=1;dp[3]=2;
for(int i=n;i>=4;i++)
{
dp[i]=dp[i-1]+dp[i-1];
}
cout<<dp[n]<<endl;
}
}

浙公网安备 33010602011771号