如何在表格中显示数字(0 1 1 2 3 5 8 13)这样显示数字前1个数字然后第二个是加上先前的数字=> 0然后是1然后1 + 0 = 1然后1 + 1 = 2然后1 + 2 = 3等等?
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 20; i++)
{
Console.Write(i) ;
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
请提供一些代码
将您的解决方案分解为Fibonacci序列生成器:
using System.Numerics;
using System.Linq;
public static IEnumerable<BigInteger> Fibonacci() {
BigInteger a = 0;
BigInteger b = 1;
yield return a;
yield return b;
while (true) {
BigInteger result = a + b;
a = b;
b = result;
yield return result;
}
}
Run Code Online (Sandbox Code Playgroud)
和序列表示:
Console.Write(String.Join(" ", Fibonacci().Take(20)));
Run Code Online (Sandbox Code Playgroud)