方法①:【递归调用】 public 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); } 方法②:【for循环】 public int AddNum(int x) { int f1 = 1; int f2 = 1; int f3 = 0; for (int j = 0; j < x; j++) { if (j > 1) { f3 = f2 + f1; f1 = f2; f2 = f3; } else { f3 = 1; } } return f3; }
public 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); }public 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); }
public static int TEST(int length) { if (length == 1) { return 1; } length -= 1; int a = 0; int b = 1; int index = 0; while (index < length) { index += 1; int b_ = b; b = a + b; a = b_; } return b; }
惭愧,还真不知道那叫斐波那契数列,学习了。我算的结果是832040 int[] ints=new int[30]; ints[0] = 1; ints[1] = 1; int c = 0; for (int i = 2; i < 30; i++) { ints[i]=ints[i-1]+ints[i-2]; c = ints[i]; } Console.WriteLine(c);
小学 大学都没学好 哈哈果断google baidu
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace 实验1 { class f1 { int s=0; public int f(int n) { if (n == 0 | n == 1) { s = 1; return s; } else { s = f(n - 1)+f(n - 2); } return s; }
} class Program { static void Main(string[] args) { int a; f1 one = new f1(); a= one.f(6); Console.WriteLine(a);
} } }
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace 实验1 { class f1 { int s=0; public int f(int n) { if (n == 0 | n == 1) { s = 1; return s; } else { s = f(n - 1)+f(n - 2); } return s; }
} class Program { static void Main(string[] args) { int a; f1 one = new f1(); a= one.f(6); Console.WriteLine(a);
} } }
楼主请看下:using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace 递归 { class Program { static void Main(string[] args) { // 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。 int n = Sum(30); Console.WriteLine(n); Console.ReadKey(); } static int Sum(int num) { int sum = 1; for (int i = num; i >=2; i--) { sum = Sum(num - 1) + Sum(num - 2); } return sum; } } }
{
if(x==1 || x==2)
return 1;
else
return fun(x-1)+fun(x-2)
}
public int feibo( int n )
{
if(n==1) return 1;
if(n==2) return 1;
return feibo(n-2) + feibo(n-1);
}
public int foo(nt n )
{
if(n<1) return 0;
if(n<=2) return 1;
return foo(n-1) + foo(n-2);
}
double Fibonacci = Math.Round((1 / Math.Sqrt(5)) * (Math.Pow(((1 + Math.Sqrt(5)) / 2), double.Parse(comboBox1.SelectedItem.ToString()))) - (Math.Pow(((1 - Math.Sqrt(5)) / 2), double.Parse(comboBox1.SelectedItem.ToString()))))
{
if (i < 0) return 0; else if (i > 0 && i <= 2) return 1; else return Foo(i - 1) + Foo(i - 2);
}
方法②:【for循环】 public int AddNum(int x)
{
int f1 = 1;
int f2 = 1;
int f3 = 0;
for (int j = 0; j < x; j++)
{
if (j > 1)
{
f3 = f2 + f1;
f1 = f2;
f2 = f3;
}
else
{
f3 = 1;
}
}
return f3; }
{
if (i < 0) return 0; else if (i > 0 && i <= 2) return 1; else return Foo(i - 1) + Foo(i - 2);
}public 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);
}
public static int TEST(int length)
{
if (length == 1)
{
return 1;
}
length -= 1;
int a = 0;
int b = 1;
int index = 0;
while (index < length)
{
index += 1;
int b_ = b;
b = a + b;
a = b_;
}
return b;
}
int[] ints=new int[30];
ints[0] = 1;
ints[1] = 1;
int c = 0;
for (int i = 2; i < 30; i++)
{
ints[i]=ints[i-1]+ints[i-2];
c = ints[i];
}
Console.WriteLine(c);
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 实验1
{
class f1
{
int s=0;
public int f(int n)
{
if (n == 0 | n == 1)
{
s = 1;
return s;
}
else
{
s = f(n - 1)+f(n - 2); }
return s;
}
}
class Program
{
static void Main(string[] args)
{
int a;
f1 one = new f1();
a= one.f(6);
Console.WriteLine(a);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 实验1
{
class f1
{
int s=0;
public int f(int n)
{
if (n == 0 | n == 1)
{
s = 1;
return s;
}
else
{
s = f(n - 1)+f(n - 2); }
return s;
}
}
class Program
{
static void Main(string[] args)
{
int a;
f1 one = new f1();
a= one.f(6);
Console.WriteLine(a);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace 递归
{
class Program
{
static void Main(string[] args)
{
// 一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
int n = Sum(30);
Console.WriteLine(n);
Console.ReadKey();
} static int Sum(int num)
{
int sum = 1;
for (int i = num; i >=2; i--)
{
sum = Sum(num - 1) + Sum(num - 2);
}
return sum;
}
}
}