使用linq生成斐波那契数列
时间: 2024-02-04 13:09:20 浏览: 150
以下是使用LINQ生成斐波那契数列的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
namespace day01
{
class Program
{
static void Main(string[] args)
{
int count = 10; // 生成斐波那契数列的个数
IEnumerable<long> fibonacciSequence = GenerateFibonacciSequence(count);
Console.WriteLine("斐波那契数列的前{0}个元素:", count);
foreach (long number in fibonacciSequence)
{
Console.WriteLine(number);
}
Console.ReadKey();
}
static IEnumerable<long> GenerateFibonacciSequence(int count)
{
long a = 0;
long b = 1;
for (int i = 0; i < count; i++)
{
yield return a;
long temp = a;
a = b;
b = temp + b;
}
}
}
}
```
阅读全文