当前位置:  编程技术>.net/c#/asp.net

c#斐波那契数列(Fibonacci)(递归,非递归)实现代码

    来源: 互联网  发布时间:2014-10-19

    本文导语:  //Main 代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Fibonacci{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("Would you like to know which Fibonacci Num...

//Main

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Would you like to know which Fibonacci Numbers:");
            int number = Convert.ToInt32(Console.ReadLine());
            //
            Function obj = new Function();
            Console.WriteLine();
            Console.Write("The {0} Fibonacci number is:{1}", number, obj.Fibonacci(number));
            //
            Console.WriteLine();
            Function obj2 = new Function(number);
            Console.Write("The {0} Fibonacci number is:{1}", number, obj2.BottomUpNotRecursion(number));
            //
            Console.WriteLine();
            Console.Write("The {0} Fibonacci number is:{1}", number, obj2.TopDownRecursion(number));
            Console.ReadKey();

        }
    }
}


//Class

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fibonacci
{
    class Function
    {
        private int[] array;

        public Function()
        {

        }

        ///
        /// Function
        ///
        ///
        public Function(int length)
        {
            if (length > 0)
            {
                array = new int[length + 1];
                array[0] = 1;
                array[1] = 1;
            }
            if (length == 0)
            {
                array = new int[1];
                array[0] = 1;
            }
        }

        ///
        /// Fibonacci数列定义为:
        ///             无穷数列1,1,2,3,5,8,13,21,34,55,……
        ///        ┌ 1             n=0    
        ///   F(n)=│ 1             n=1
        ///        └ F(n-1)+F(n-2) n>1
        ///
        /// 第几个斐波那契数
        ///
        public int Fibonacci(int number)
        {
            if (number


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。




特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3