日期:2014-05-17  浏览次数:20741 次

c#控制台应用 输出斐波那契数 求指教!
新人求指教

------解决方案--------------------
C# code
public class MainClass 
                  { 
                  public static void Main() 
                  { 
                  Console.WriteLine(Foo(30)); 
                  } 
                  public static int Foo(int i) 
                  { 
                  if (i <= 0) 
                  return 0; 
                  else if(i > 0 && i <= 2) 
                  return 1; 
                  else return Foo(i -1) + Foo(i - 2); 
                  } 
                  }

------解决方案--------------------
C# code
 static void Main(string[] args)
        {

            Console.WriteLine(fab(3));//n表示第几个斐波那契数



        }
      static  int fab(int n)
        {
            if (n == 1 || n == 2)
                return 1;
            else
                return fab(n - 1) + fab(n - 2);
        }

------解决方案--------------------
C# code

public class MainClass 
                  { 
                  public static void Main() 
                  { 
                  Console.WriteLine(Foo(10)); 
                  } 
                  public static int Foo(int i) 
                  { 
                  if (i <= 0) 
                  return 0; 
                  else if(i > 0 && i <= 2) 
                  Console.WriteLine("1 "); 
                  else 
                  Console.WriteLine((Foo(i -1) + Foo(i - 2)).ToString()); 
    
                  } 
                  }

------解决方案--------------------
C# code
int a = 1, b = 1;
            
            for (int i = 0; i < 10; i++)
            {
                int temp = a;
                if (i < 2)
                    Console.WriteLine(temp);
                else
                {
                    Console.WriteLine(a+b);
                    a = b;
                    b = temp + b;
                   
                }
            }