Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 视觉工作室中的斐波那契数组序列_C# - Fatal编程技术网

C# 视觉工作室中的斐波那契数组序列

C# 视觉工作室中的斐波那契数组序列,c#,C#,现在要更改它,使每个数字都是最后2个数字的和,使每个数字都是最后3个数字的和 这个系列的前3个数字是0,1,2 static void PrintFibonocciNumbers(int count) { int[] result = GetFibonocciNumbers(count); PrintArray(result); } static int[] GetFibonocciNumbers(int count) {

现在要更改它,使每个数字都是最后2个数字的和,使每个数字都是最后3个数字的和 这个系列的前3个数字是0,1,2

    static void PrintFibonocciNumbers(int count)
    {
        int[] result = GetFibonocciNumbers(count);
        PrintArray(result);
    }

    static int[] GetFibonocciNumbers(int count)
    {
        int[] result = new int[count];

        for (int i = 0; i< count; ++i)
        {
            //need a special case if i == 0...we know for 0, the number is 0...so
            //also need a special case if i == 1...we know that will be 1
            // so manually set the first 2 values to the numbers that we know...0 and 1
            // then the rest can be calculated automaticlly
            if(i <= 1)
            {
                result[i] = i;
            }
            else
            {
                result[i] = result[i - 3] + result[i - 1];
            }

            //lets run it through the debugger
        }

        return result;
    }

    static int AddInts(int a, int b)
    {
        int c = a + b;

        return c;
    }
}
static void PrintFibonocciNumbers(int计数)
{
int[]结果=GetFibonocciNumbers(计数);
打印阵列(结果);
}
静态int[]GetFibonocciNumbers(int计数)
{
int[]结果=新的int[计数];
对于(int i=0;i如果(i我会使用IEnumerable生成序列。在我看来,它更容易表达:

public static IEnumerable<int> GetSequence()
{
        //define the initial values for the iteration
        int first = 0;
        int second = 1;
        int next = 2;

        //return the first 3 items
        yield return first;
        yield return second;
        yield return next;

        //loop forever (in reality, we might overflow) you can also limit this to
        //a specific n
        while (true)
        {
            //shift the 3 values, losing the first one (we no longer need it)
            (first, second, next) = (second, next, first + second + next);
            //return our result and yield the thread
            yield return next;
        }
}

//take the first 15 values of the IEnumerable
foreach (var val in GetSequence().Take(15))
{
  Console.WriteLine(val);
}
公共静态IEnumerable GetSequence()
{
//定义迭代的初始值
int first=0;
int秒=1;
int-next=2;
//返回前3项
收益率优先;
收益率第二;
其次是收益率;
//永远循环(实际上,我们可能会溢出)您也可以将此限制为
//特定的
while(true)
{
//移动3个值,丢失第一个值(我们不再需要它)
(第一,第二,下一)=(第二,下一,第一+第二+下一);
//返回结果并生成线程
其次是收益率;
}
}
//取IEnumerable的前15个值
foreach(GetSequence()中的var val.Take(15))
{
控制台写入线(val);
}

我实际上会使用IEnumerable生成序列。在我看来,它更容易表达:

public static IEnumerable<int> GetSequence()
{
        //define the initial values for the iteration
        int first = 0;
        int second = 1;
        int next = 2;

        //return the first 3 items
        yield return first;
        yield return second;
        yield return next;

        //loop forever (in reality, we might overflow) you can also limit this to
        //a specific n
        while (true)
        {
            //shift the 3 values, losing the first one (we no longer need it)
            (first, second, next) = (second, next, first + second + next);
            //return our result and yield the thread
            yield return next;
        }
}

//take the first 15 values of the IEnumerable
foreach (var val in GetSequence().Take(15))
{
  Console.WriteLine(val);
}
公共静态IEnumerable GetSequence()
{
//定义迭代的初始值
int first=0;
int秒=1;
int-next=2;
//返回前3项
收益率优先;
收益率第二;
其次是收益率;
//永远循环(实际上,我们可能会溢出)您也可以将此限制为
//特定的
while(true)
{
//移动3个值,丢失第一个值(我们不再需要它)
(第一,第二,下一)=(第二,下一,第一+第二+下一);
//返回结果并生成线程
其次是收益率;
}
}
//取IEnumerable的前15个值
foreach(GetSequence()中的var val.Take(15))
{
控制台写入线(val);
}

…你被困在哪里?给我们一些关于你的思维过程的细节。有错误吗?什么在起作用或不起作用?…看起来你需要做的就是改变
如果(我…你被困在哪里?给我们一些关于你的思维过程的细节。有错误吗?什么在起作用或不起作用?…看起来你需要做的就是改变
如果如果(i@user10964630)在我的代码中,你可能应该用1而不是2初始化next..我现在不在计算机旁。如果你需要我修改你的代码,我可以,但可能明天(i@user10964630在我的代码中,你可能应该用1而不是2初始化next..我现在不在计算机旁边。如果你需要我修改你的代码,我可能明天就可以了