2 solutions

  • 0
    @ 2024-11-24 22:51:29
    #include <stdio.h>
    #include <stdlib.h>
    #define Max_N 100
    int memo[Max_N+1];
    void init_memo(){
        for(int i=0;i<Max_N;i++){
            memo[i]=-1;
        }
    }
    int fib(n){
        if(n==0) return 0;
        if(n==1||n==2) return 1;
        if(memo[n]!=-1){
            return memo[n];
        }
        memo[n]=fib(n-2)+fib(n-1);
        return memo[n];
    }
    int main(void){
        int n;
        init_memo();
        scanf("%d",&n);
        if(n<0){
            return -1;
        }
        printf("%d",fib(n-1));
        return 0;
    }
    

    Information

    ID
    216
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    4
    Accepted
    2
    Uploaded By