1 solutions

  • 0
    @ 2024-8-29 22:02:40

    两种方法: 第一种: 斐波那契数列:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	double f[50];
    	int n;
    	cin >> n;
    	f[0] = 0;
    	f[1] = 1;
    	f[2] = 1;
    	for (int i = 3; i <= n; i++) {
    		f[i] = f[i - 1] + f[i - 2];
    	}
    	printf("%0.2lf", f[n]);
    	return 0;
    }
    

    第二种: 直接用题目中给的公式硬算:

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
    	double n;
    	cin>>n;
    	printf("%0.2lf",(pow(((1+sqrt(5))/2),n)-pow(((1-sqrt(5))/2),n))/sqrt(5));
    	return 0;
    }
    

    👍

    • 1

    【深基4.习4】月落乌啼算钱(斐波那契数列)

    Information

    ID
    68
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    9
    Tags
    # Submissions
    7
    Accepted
    6
    Uploaded By