hbw 2024/7/15

#include <iostream>
using namespace std;
long long memo[100000];
int fibo(int n){
	if(n==1 ||n==2){
		return 1;
	}
	cout<<fibo(n-1)+fibo(n-2)<<endl;
	return fibo(n-1)+fibo(n-2);
}
int main(){
	int n=0;
    cout<<"请输入月份数:";
    cin>>n;
    cout<<fibo(n);
}
#include <iostream>
using namespace std;
long long memo[100000]={0};
long long fibo(long long n,long long memo[]){
	if(n==1 ||n==2){
		memo[n]=1;
		return 1;
	}
	if(memo[n]!=0){
		return memo[n];
	}else{
		memo[n]=fibo(n-1,memo)+fibo(n-2,memo);
		return memo[n];
	}
}
int main(){
	long long n=0;
    cout<<"请输入月份数:";
    cin>>n;
    cout<<fibo(n,memo);
} 

1 条评论

  • @ 2024-7-15 16:52:52
    #include <iostream>
    using namespace std;
    int fibo(int n){
        if (n == 1 || n == 2){
            return 1;
        }
        return fibo(n-1) + fibo(n-2);
        return 0;
    }
    int main(){
        int n;
        cin >> n;
        cout << "第" << n << "个月有" << fibo(n) << "只🐰";
        return 0;
    }
    
    • 1