斐波那契数列嘛,一搜就有了
——————————————————————————————————
static void Main(string[] args)
{
int a = 1, b = 0, c = 0;
while (a + b <= 10000)
{
c = a + b;
a = b;
b = c;
Console.WriteLine(c);
}
Console.Read();
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
static void Main(string[] args)
{
ArrayList al= GetNums();
foreach (var num in al)
{
Console.WriteLine(num.ToString());
}
Console.Read();
}
static ArrayList GetNums()
{
ArrayList al = new ArrayList();
for (int k = 1; k < 10; k++)
{
if (GetNums(k) < 10000)
{
al.Add(GetNums(k));
}
}
return al;
}
static int GetNums(int index)
{
if (index == 1)
{
return 1;
}
if (index == 2)
{
return 1;
}
return GetNums(index - 1) + GetNums(index - 2);
}
}
}
这就是一个简单的递归算法。。但是1楼的老兄没有用递归。。而且代码比较优雅。。