17 条题解

  • 13
    @ 2017-10-20 16:18:22

    三行代码过斐波那契数列!!!

    n=int(input())
    a=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040]
    print(a[n-1])
    
    
  • 7
    @ 2017-10-20 16:28:58

    高端操作之函数递归

    def fb(n):
        if n==1 or n==2:
            return 1
        else:
            return fb(n-1)+fb(n-2)
            
    n=int(input())
    print(fb(n))
    
    
    • @ 2018-10-22 23:26:15

      Time Limit Exceeded

  • 4
    @ 2019-10-31 21:46:03

    通项公式法...

    n = int(input())
    a = 5**(0.5)/5
    b = ((1+5**(0.5))/2)**(n)
    c = ((1-5**(0.5))/2)**(n)
    d = a*(b-c)
    print(int(d))
    
  • 2
    @ 2024-01-02 17:23:27
    n = int(input()); print(int((1/5**0.5)*(((1+5**0.5)/2)**n - ((1-5**0.5)/2)**n)))
    
  • 2
    @ 2023-09-23 21:35:07

    事实上打表还可以压行。。。
    两行就够

    l=[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040]
    print(l[int(input())])
    
  • 2
    @ 2019-04-15 21:40:35

    通项公式考虑一下吧
    就不写了

  • 1
    @ 2023-09-08 17:32:04

    用空间换时间
    直接打表

    n = eval(input()) - 1
    a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040]
    print(a[n])
    
  • 1
    @ 2020-09-16 23:04:15

    #python3

    n=int(input())
    while(1):
        if n<1 or n>30:
            n=int(input())
        else:
            break
    
    b=((1+5**0.5)/2)**n
    c=((1-5**0.5)/2)**n
    d=(5**0.5)
    a=(b-c)/d
    print(int(a))
    
    
    
  • 1
    @ 2019-04-15 21:39:22

    n=int(input())
    x=1
    y=0
    while n>0:
    if x>=y:
    y=x+y
    if x<y:
    x=x+y
    n=n-2
    if x>=y:
    if n==0:
    print(x)
    else:
    print(y)
    if x<y:
    if n==0:
    print(y)
    else:
    print(x)

  • 1
    @ 2019-04-14 19:49:57

    高端操作之函数递归的c++版本

    #include <iostream>
    #include <stdlib.h>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int fibe(int n)
    {
        if(n == 1 || n == 2)
        {
            return 1;
        }
        else
        {
            return fibe(n - 1) + fibe(n - 2);
        }
    }
    
    int main()
    {
        int n;
        cin >> n;
        int ans;
        ans = fibe(n);
        cout << ans;
        return 0;
    }
    
  • 0
    @ 2020-07-22 14:55:28

    n=int(input())
    a=1
    b=1
    c=1
    for i in range(n-2):
    c=a+b
    a=b
    b=c
    print(c)
    好像和别人的不太一样?

  • 0
    @ 2020-01-28 16:23:37

    a=int(input())
    def num(x):
    if x==1 or x==2:
    return 1
    else:
    return num(x-1)+num(x-2)
    print(num(a))

  • 0
    @ 2019-03-03 11:16:37

    n=int(input())
    a=1
    b=1
    if n==1 or n==2:
    print(a)
    else:
    for i in range(3,n+1):
    c=a+b
    a=b
    b=c
    print(c)

  • 0
    @ 2019-02-13 20:17:35

    #include<iostream>
    using namespace std;
    int a[10000]={1,1};
    int main()
    {
    int k;
    cin>>k;
    for(int i=2;i<=k-1;i++)
    a[i]=a[i-1]+a[i-2];
    cout<<a[k-1];
    }

  • -3
    @ 2018-04-13 09:56:19

    此题算法复杂度可以降到1...上面递归的太慢...
    首先分析数列f(n+2)=f(n+1)+f(n),f(1)=f(2)=1;
    我们易得出特征根方程:x^2=x+1,解为a,b,则有f(n)=c*a^n+d*b^n。
    其中c,d为参数,将方程代入f(1)=f(2)=1即可解出。
    因此列程序:

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int f(int n){
        if(n==1)return 1;
        double a=(sqrt(5.0)+1.0)/2.0;
        double b=(sqrt(5.0)-1.0)/2.0;
        double r=((pow(a,n)-pow(b,n))/(sqrt(5.0))+0.5);
        return (int)r;
    }
    int main(int argc, char* argv[])
    {
        int n;
        scanf("%d",&n);
        printf("%d",f(n));
        return 0;
    }
    
    
  • -6
    @ 2017-10-16 16:57:59

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main(){
    int F[46+7];
    int k;
    cin>>k;
    memset(F,0,sizeof(F));
    F[1]=1;F[2]=1;
    for(int i=3; i<=k;i++){
    F[i]=F[i-1]+F[i-2];
    }
    cout<<F[k];
    return 0;
    }

  • -7
    @ 2017-10-16 20:46:55

    A=int(input())
    b1,b2=1,1
    for i in range(A-2):
    b1,b2=b2,b1+b2
    print(b2)

  • 1

信息

难度
3
分类
(无)
标签
(无)
递交数
2232
已通过
650
通过率
29%
被复制
1
上传者