n steps with 1, 2 or 3 steps taken. How many ways to get to the top?

前端 未结 13 882
时光说笑
时光说笑 2020-12-08 08:12

If we have n steps and we can go up 1 or 2 steps at a time, there is a Fibonacci relation between the number of steps and the ways to climb them. IF and ONLY if we do not co

13条回答
  •  悲哀的现实
    2020-12-08 09:03

    Count ways to reach the nth stair using step 1, 2, 3.

    We can count using simple Recursive Methods.

    // Header File
    #include
    // Function prototype for recursive Approch
    int findStep(int);
    int main(){
        int n;
        int ways=0;
        ways = findStep(4);
        printf("%d\n", ways);
    return 0;
    }
    // Function Definition
    int findStep(int n){
        int t1, t2, t3;
        if(n==1 || n==0){
            return 1;
        }else if(n==2){
            return 2;
        }
        else{
            t3 = findStep(n-3);
            t2 = findStep(n-2);
            t1 = findStep(n-1);
            return t1+t2+t3;
        }
    }
    

提交回复
热议问题