C# 返回序列的第n个斐波那契数?

C# 返回序列的第n个斐波那契数?,c#,iteration,fibonacci,C#,Iteration,Fibonacci,我有一个关于课堂作业的问题,我需要知道如何使用迭代(不允许递归)返回第n个斐波那契序列 我需要一些如何做到这一点的技巧,这样我才能更好地理解我做错了什么。我在program.cs中输出到控制台,因此它在下面的代码中不存在 // Q1) // // Return the Nth Fibonacci number in the sequence // // Input: uint n (which number to get) // Output: T

我有一个关于课堂作业的问题,我需要知道如何使用迭代(不允许递归)返回第n个斐波那契序列

我需要一些如何做到这一点的技巧,这样我才能更好地理解我做错了什么。我在program.cs中输出到控制台,因此它在下面的代码中不存在

    // Q1)
    //
    // Return the Nth Fibonacci number in the sequence
    //
    // Input: uint n (which number to get)
    // Output: The nth fibonacci number
    //

    public static UInt64 GetNthFibonacciNumber(uint n)
    {

    // Return the nth fibonacci number based on n.


    if (n == 0 || n == 1)
        {
            return 1;
        }

        // The basic Fibonacci sequence is 
        // 1, 1, 2, 3, 5, 8, 13, 21, 34...
        // f(0) = 1
        // f(1) = 1
        // f(n) = f(n-1) + f(n-2)
        ///////////////
        //my code is below this comment

        uint a = 0;
        uint b = 1;

        for (uint i = 0; i < n; i++)
        {
            n = b + a;
            a = b;
            b = n;
        }
        return n;
/Q1)
//
//返回序列中的第n个斐波那契数
//
//输入:uint n(要获取的数字)
//输出:第n个斐波那契数
//
公共静态UInt64 GetNthFibonacciNumber(uint n)
{
//返回基于n的第n个斐波那契数。
如果(n==0 | | n==1)
{
返回1;
}
//基本的斐波那契序列是
// 1, 1, 2, 3, 5, 8, 13, 21, 34...
//f(0)=1
//f(1)=1
//f(n)=f(n-1)+f(n-2)
///////////////
//我的代码就在这条评论下面
uint a=0;
uint b=1;
对于(uint i=0;i
我认为这应该可以做到:

    uint a = 0;
    uint b = 1;
    uint c = 1;

    for (uint i = 0; i < n; i++)
    {
        c = b + a;
        a = b;
        b = c;
    }
    return c;
uint a=0;
uint b=1;
uint c=1;
对于(uint i=0;i
:)


只是为了好玩,你可以用一个无限的斐波那契列表和一些IEnumerable扩展

public IEnumerable<int> Fibonacci(){
   var current = 1;
   var b = 0;
   while(true){
       var next = current + b;
       yield return next;
       b = current;
       current = next;
   }
}

public T Nth<T>(this IEnumerable<T> seq, int n){
    return seq.Skip.(n-1).First();
}

这是你家庭作业的解决方案,你应该从3开始,因为你已经有了f1和f2的数字(前两个数字)。请注意,获得第0个斐波那契数字没有意义

public static UInt64 GetNthFibonacciNumber(uint n)
    {

    // Return the nth fibonacci number based on n.


if (n == 1 || n == 2)
    {
        return 1;
    }


    uint a = 1;
    uint b = 1;
    uint c;

    for (uint i = 3; i <= n; i++)
    {
        c = b + a;
        a = b;
        b = c;
    }
    return c;
publicstaticuint64 getnthfilbonaccinumber(uintn)
{
//返回基于n的第n个斐波那契数。
如果(n==1 | | n==2)
{
返回1;
}
uint a=1;
uint b=1;
uint c;
对于(uint i=3;i
public static UInt64 getnthfibonaccidenumber(uint n)
{
如果(n==0 | | n==1)
{
返回1;
}
UInt64 a=1,b=1;
uint i=2;
而(ib)b+=a;
否则a+=b;
++一,;
}
返回(a>b)?a:b;
}
公共静态int GetNthFibonacci(int n)
{
var previous=-1;
无功电流=1;
int指数=1;
int元素=0;
while(index++
公共静态列表PrintFibonacci(整数)
{
列表结果=新列表();
如果(数字==0)
{
结果:添加(0);
返回结果;
}
else if(数字==1)
{
结果:添加(0);
返回结果;
}
else if(数字=2)
{
AddRange(新列表(){0,1});
返回结果;
}
其他的
{
//如果到目前为止,我们应该把f1,f2和f3作为斐波那契数
int f1=0,
f2=1;
AddRange(新列表(){f1,f2});
for(int i=2;i
只需要两个变量(在for循环计数中声明一个)

如果要查看序列n,请将其更改为:

public IEnumerable<int> NthFib(int n)
{
    int curFib = 0;
    int nextFib = 1;
    while (n-- > 0)
    {
        yield return curFib;
        nextFib += curFib;
        curFib = nextFib - curFib;
    }
}
public IEnumerable NthFib(int n)
{
int curFib=0;
int-nextFib=1;
而(n-->0)
{
收益率;
nextFib+=curFib;
curFib=nextFib-curFib;
}
}

您正在重复使用
n
。这使得循环条件在第一次迭代后出现错误。您不应该在for循环中修改
n
。哇,我觉得自己很笨谢谢您,新手programming@user1766351我们都有过这样的经历。或者至少我们大多数人都有过这样的经历。你应该给你的变量起一个更有意义的名字,而不是a,b,n等等t将有助于缓解这样的问题。这使得fib(1)=2。我认为你应该将a改为0,c=1。这样,fib(0)=c=1,fib(1)仍然等于1,fib(2)=2,我认为这是正确的顺序为什么不呢,那么,就从1开始I?他已经得到了0的捕获,1他可以移除捕获:)很好的解决方案,但是OP说程序应该使用迭代,所以这不符合条件。对不起;-)@Emile我知道,我发布这篇文章是为了好玩。另外,
n2/sqrt5
将始终是这项工作的原因:随着数字变大,它开始崩溃,并返回不正确的结果。
public IEnumerable<int> Fibonacci(){
   var current = 1;
   var b = 0;
   while(true){
       var next = current + b;
       yield return next;
       b = current;
       current = next;
   }
}

public T Nth<T>(this IEnumerable<T> seq, int n){
    return seq.Skip.(n-1).First();
}
Fibonacci().Nth(n);
public static UInt64 GetNthFibonacciNumber(uint n)
    {

    // Return the nth fibonacci number based on n.


if (n == 1 || n == 2)
    {
        return 1;
    }


    uint a = 1;
    uint b = 1;
    uint c;

    for (uint i = 3; i <= n; i++)
    {
        c = b + a;
        a = b;
        b = c;
    }
    return c;
    public static UInt64 GetNthFibonacciNumber(uint n)
    {
        if (n == 0 || n == 1)
        {
            return 1;
        }
        UInt64 a = 1, b = 1;
        uint i = 2;
        while (i <= n)
        {
            if (a > b) b += a;
            else a += b;
            ++i;
        }
        return (a > b) ? a : b;
    }
        public static int GetNthFibonacci(int n)
    {
        var previous = -1;
        var current = 1;
        int index = 1;
        int element = 0;

        while (index++ <= n)
        {
            element = previous + current;
            previous = current;
            current = element;
        }

        return element;
    }
 public static List<int> PrintFibonacci(int number)
        {
            List<int> result = new List<int>();
            if (number == 0)
            {
                result.Add(0);
                return result;
            }
            else if (number == 1)
            {
                result.Add(0);
                return result;
            }
            else if (number == 2)
            {
                result.AddRange(new List<int>() { 0, 1 });
                return result;
            }
            else
            {
                //if we got thus far,we should have f1,f2 and f3 as fibonacci numbers
                int f1 = 0,
                    f2 = 1;

                result.AddRange(new List<int>() { f1, f2 });
                for (int i = 2; i < number; i++)
                {
                    result.Add(result[i - 1] + result[i - 2]);
                }
            }
            return result;

        }
public int NthFib(int n)
{
    int curFib = 0;
    int nextFib = 1;
    while (--n > 0)
    {
        nextFib += curFib;
        curFib = nextFib - curFib;
    }
    return curFib;
}

public IEnumerable<int> NthFib(int n)
{
    int curFib = 0;
    int nextFib = 1;
    while (n-- > 0)
    {
        yield return curFib;
        nextFib += curFib;
        curFib = nextFib - curFib;
    }
}