Finding all paths down stairs?

后端 未结 13 1515
天命终不由人
天命终不由人 2020-12-13 07:21

I was given the following problem in an interview:

Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible way

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-13 08:08

    Late C-based answer

    #include 
    #include 
    #define steps 60
    static long long unsigned int MAP[steps + 1] = {1 , 1 , 2 , 0,};
    
    static long long unsigned int countPossibilities(unsigned int n) {
        if (!MAP[n]) {
           MAP[n] = countPossibilities(n-1) + countPossibilities(n-2);
        }
        return MAP[n];
    }
    
    int main() {
       printf("%llu",countPossibilities(steps));
    }
    

提交回复
热议问题