📌  相关文章
📜  计算到达第 n 个楼梯的方法的 C# 程序

📅  最后修改于: 2022-05-13 01:56:12.169000             🧑  作者: Mango

计算到达第 n 个楼梯的方法的 C# 程序

有n个楼梯,站在底部的人想爬到顶部。此人一次可以爬 1 个楼梯或 2 个楼梯。数一数路数,人能登顶。

楼梯

考虑图中所示的示例。 n 的值为 3。有 3 种方法可以到达顶部。该图取自 Easier Fibonacci 谜题

C#
// C# program to count the
// number of ways to reach
// n'th stair
using System;
  
class GFG {
    // A simple recursive
    // program to find n'th
    // fibonacci number
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
  
    // Returns number of ways
    // to reach s'th stair
    static int countWays(int s)
    {
        return fib(s + 1);
    }
  
    // Driver Code
    static public void Main()
    {
        int s = 4;
        Console.WriteLine("Number of ways = " + countWays(s));
    }
}
  
// This code is contributed
// by akt_mit


C#
// C# program to Count ways to reach
// the n’th stair
using System;
  
class GFG {
  
    // A recursive function used by
    // countWays
    static int countWaysUtil(int n, int m)
    {
        if (n <= 1)
            return n;
        int res = 0;
  
        for (int i = 1; i <= m && i <= n; i++)
            res += countWaysUtil(n - i, m);
        return res;
    }
  
    // Returns number of ways to reach
    // s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    /* Driver program to test above function */
    public static void Main()
    {
        int s = 4, m = 2;
        Console.Write("Number of ways = "
                      + countWays(s, m));
    }
}
  
// This code is contributed by nitin mittal.


C#
// C# program to count number
// of ways to reach n't stair when
// a person can climb 1, 2, ..m
// stairs at a time
using System;
class GFG {
  
    // A recursive function
    // used by countWays
    static int countWaysUtil(int n, int m)
    {
        int[] res = new int[n];
        res[0] = 1;
        res[1] = 1;
        for (int i = 2; i < n; i++) {
            res[i] = 0;
            for (int j = 1; j <= m && j <= i; j++)
                res[i] += res[i - j];
        }
        return res[n - 1];
    }
  
    // Returns number of ways
    // to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    // Driver Code
    public static void Main()
    {
        int s = 4, m = 2;
        Console.WriteLine("Number of ways = " + countWays(s, m));
    }
}
  
// This code is contributed by anuj_67.


输出:
Number of ways = 5

上述实现的时间复杂度是指数级的(黄金比例提高到 n 次方)。可以使用前面讨论的斐波那契函数优化来优化它以在 O(Logn) 时间内工作。

C#

// C# program to Count ways to reach
// the n’th stair
using System;
  
class GFG {
  
    // A recursive function used by
    // countWays
    static int countWaysUtil(int n, int m)
    {
        if (n <= 1)
            return n;
        int res = 0;
  
        for (int i = 1; i <= m && i <= n; i++)
            res += countWaysUtil(n - i, m);
        return res;
    }
  
    // Returns number of ways to reach
    // s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    /* Driver program to test above function */
    public static void Main()
    {
        int s = 4, m = 2;
        Console.Write("Number of ways = "
                      + countWays(s, m));
    }
}
  
// This code is contributed by nitin mittal.
输出:
Number of ways = 5

C#

// C# program to count number
// of ways to reach n't stair when
// a person can climb 1, 2, ..m
// stairs at a time
using System;
class GFG {
  
    // A recursive function
    // used by countWays
    static int countWaysUtil(int n, int m)
    {
        int[] res = new int[n];
        res[0] = 1;
        res[1] = 1;
        for (int i = 2; i < n; i++) {
            res[i] = 0;
            for (int j = 1; j <= m && j <= i; j++)
                res[i] += res[i - j];
        }
        return res[n - 1];
    }
  
    // Returns number of ways
    // to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    // Driver Code
    public static void Main()
    {
        int s = 4, m = 2;
        Console.WriteLine("Number of ways = " + countWays(s, m));
    }
}
  
// This code is contributed by anuj_67.
输出:
Number of ways = 5

有关详细信息,请参阅有关到达第 n 级楼梯的计数方式的完整文章!