using System;

 

namespace 菲波那契数列

{

//     class classMethod1    //这个方法是求出菲波那契数列的第n个项

//     {

//            public static int Method( int n )

//            {

//                   if( n==1 || n==2)

//                          return 1;

//                   else

//                          return Method( n-1 ) + Method( n-2 );

//            }

//     }

//     class MainClass

//     {                  

//            static void Main(string[] args)

//            {

//                   Console.WriteLine("请输入一个大于3的整数数字,求出菲波那契数列的第n个项:");

//                   int n = Convert.ToInt32(Console.ReadLine());

//                   Console.WriteLine(classMethod1.Method(n));

//            }

//     }

 

       class classMethod2    //这个方法是求出最后一项不大于n的菲波那契数列

       {

              public static void Method()

              {

                     Console.WriteLine("请输入一个大于3的整数数字,求出以前两项为1的菲波那契数列:");

                     int n = Convert.ToInt32(Console.ReadLine());

                     int oldNumber = 1;

                     int currentNumber = 1;   //current : 当前的,现在的,最近的

                     int nextNumber;

                     Console.Write("1 ");

                     while (currentNumber <= n)

                     {

                            System.Console.Write(currentNumber + " ");

                            nextNumber = currentNumber + oldNumber;

                            oldNumber = currentNumber;

                            currentNumber = nextNumber;

                     }

              }

       }

       class MainClass

       {

              public static void Main()

              {

                     classMethod2.Method();

              }

       }

 

}

posted on 2008-11-13 00:35  〆o殺殸纨o〆  阅读(253)  评论(0编辑  收藏  举报