瀏覽標籤:

基礎複習

[C#] 基礎複習 – 遞迴練習

好久沒用遞迴了,趕快找個簡單的題目複習一下,不然都要忘光光了,熟悉一下之前的Feel ~
順便練習一下簡單工廠模式,首先設計介面ICompute.cs

interface ICompute
{
    /// <summary>
    /// 1 + ... + N = ?
    /// </summary>
    /// <param name="n"></param>
    /// <returns>Answer</returns>
    Int64 Iterative(Int64 n);

    /// <summary>
    /// 1 - 2 + 3 ... + N = ?
    /// </summary>
    /// <param name="n"></param>
    /// <returns>Answer</returns>
    Int64 AdvancedIterative(Int64 n);

    /// <summary>
    /// 1 x 2 x ... x N = ?
    /// </summary>
    /// <param name="n"></param>
    /// <returns>Answer</returns>
    Int64 Factorial(Int64 n);

    /// <summary>
    /// Fn = Fn-1 + Fn-2
    /// </summary>
    /// <param name="n"></param>
    /// <returns>Answer</returns>
    Int64 Fibonacci(Int64 n);
}

接著建立兩個類別Recursive.csForLoop.cs並繼承ICompute.cs實作其方法:

  • Recursive.cs
    public class Recursive : ICompute
    {
        public Int64 Iterative(Int64 n)
        {
            if (n == 1)
            {
                return n;
            }
            else
            {
                return n + Iterative(n - 1);
            }
        }
    
        public Int64 AdvancedIterative(Int64 n)
        {
            if (n == 1)
            {
                return n;
            }
            else
            {
                return n % 2 == 0 ? -n + AdvancedIterative(n - 1) : n + AdvancedIterative(n - 1);
            }
        }
    
        public Int64 Factorial(Int64 n)
        {
            if (n == 1)
            {
                return n;
            }
            else
            {
                return n * Factorial(n - 1);
            }
        }
    
        public Int64 Fibonacci(Int64 n)
        {
            if (n < 3)
            {
                return 1;
            }
            else
            {
                return Fibonacci(n - 1) + Fibonacci(n - 2);
            }
        }
    }
  • ForLoop.cs
    public class ForLoop : ICompute
    {
    
        public Int64 Iterative(Int64 n)
        {
            int ans = 0;
    
            for (int i = 1; i <= n; i++)
            {
                ans += i;
            }
    
            return ans;
        }
    
        public Int64 AdvancedIterative(Int64 n)
        {
            int ans = 0;
    
            for (int i = 1; i <= n; i++)
            {
                if (i % 2 == 0)
                {
                    ans -= i;
                }
                else
                {
                    ans += i;
                }
            }
    
            return ans;
        }
    
        public Int64 Factorial(Int64 n)
        {
            int ans = 1;
    
            for (int i = 1; i <= n; i++)
            {
                ans *= i;
            }
    
            return ans;
        }
    
        public Int64 Fibonacci(Int64 n)
        {
            int ans = 0;
            int[] arr = new int[2] { 1, 1 };
    
            for (int i = 1; i <= n; i++)
            {
                if (i < 3)
                {
                    ans = 1;
                }
                else
                {
                    ans = arr[0] + arr[1];
                    arr[0] = arr[1];
                    arr[1] = ans;
                }
            }
    
            return ans;
        }
    }

接著在Program.cs裡面寫一個Run的方法來呼叫剛剛寫好的兩個類別:

static void Run(int n, ICompute compute)
{
    Console.WriteLine("1 + ... + N = {1}", n, compute.Iterative(n));
    Console.WriteLine("1 - 2 + 3 ... + N = {1}", n, compute.AdvancedIterative(n));
    Console.WriteLine("N! = {1}", n, compute.Factorial(n));
    Console.WriteLine("Fn = Fn-1 + Fn-2, n = {0}, Ans = {1}", n, compute.Fibonacci(n));
}

這樣就完成囉 !!

2017-01-05-20_15_29-file____c__users_developer_documents_recursiveeample_recursiveeample_recursiveea

 

原始碼:https://github.com/shuangrain/RecursiveEample