如何在表格中显示数字(0 1 1 2 3 5 8 13)

-3 c# console-application

如何在表格中显示数字(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)

请提供一些代码

Dmi*_*nko 5

将您的解决方案分解为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)

  • 很好地使用`BigInteger`,`yield`和`Take()`来让OP的作业看起来像是从网上复制它.但实际上,你不应该回答像这样的问题.如何计算C#中的Fibonacci数可以在网上的数千个地方找到,Stack Overflow肯定有重复,OP的问题非常不清楚(对于_what_ help他们实际上正在寻找). (3认同)