4 solutions

  • 4
    @ 2024-1-26 18:32:53
    #include<iostream>
    using namespace std;
    int f(int x)
    {
    	if(x==1)//等于1时
    	{
    		return 1;
    	}
    	if(x==2)//等于2时
    	{
    		return 1;
    	}
    	return f(x-1)+f(x-2);//两项之和
    }
    int main()
    {
    	int n;
    	cin>>n;//输入
    	for(int i=0;i<n;i++)
    	{
    		int x;
    		cin>>x;//输入
    		cout<<f(x)<<"\n";//输出
    	}
    	return 0;//完结散花
    }
    
    • 3
      @ 2024-1-23 9:47:54
      #include<bits/stdc++.h>
      using namespace std;
      int n;
      int a;
      int f(int w){
      	if(w<=2) return 1;
      	return f(w-2)+f(w-1);
      }
      int main(){
      	system("color F1");
      	cin>>n;
      	for(int x=0;x<n;x++){
      		cin>>a;
      		cout<<f(a)<<endl;
      	}
      	return 0;
      }
      

      TLE功德佛

    • 2
      @ 2024-1-23 10:01:44

      其实很简单 对斐波那契的每一项预处理即可,最后输出应用即可

      #include<bits/stdc++.h>
      using namespace std;
      
      int a[25],num[25];
      
      int main()
      {
          int n;
          cin >> n;
          a[1] = a[2] = 1;
      	for(int i=3;i<=50;i++)
      	    a[i] = a[i - 1] + a[i - 2]; 
          for(int i = 1; i <= n; i++)
          	cin >> num[i];
      	for(int i = 1; i <= n; i++)
      		cout << a[num[i]] << endl;
          return 0;
      }
      
      • 1
        @ 2024-1-23 10:14:10
        #include<iostream>
        #include<cstdio>
        using namespace std;
        int fib(int n)
        {
            if(n==1 || n==2) return 1;
            else return fib(n-1)+fib(n-2);
        }
        int main()
        {
            int i,n,m,tot;
            cin>>n;
            for(i=0;i<n;i++)
            {
                cin>>m;
                tot=fib(m);
                cout<<tot<<endl;;
            }
        }
        
        • 1

        Information

        ID
        687
        Time
        1000ms
        Memory
        256MiB
        Difficulty
        2
        Tags
        # Submissions
        72
        Accepted
        45
        Uploaded By