日期:2014-05-17 浏览次数:20741 次
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); } }
------解决方案--------------------
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); }
------解决方案--------------------
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()); } }
------解决方案--------------------
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; } }