【问题标题】:need help correcting my code for fibonacci sequence需要帮助纠正我的斐波那契数列代码
【发布时间】:2019-09-21 01:23:37
【问题描述】:

我需要创建一个程序,要求用户输入整数 N,然后使用斐波那契数列的 void 函数打印出斐波那契数列的前 N ​​项。在一个实例之后,程序必须询问用户是否希望继续。如果用户回答 Y,程序必须向用户询问另一个整数 N,然后打印出斐波那契数列的前 N ​​项,依此类推。

我已经为此编写了代码。它在第一个实例中起作用。问题是,如果用户选择再做一次,结果就不再是正确的斐波那契数列了。下面是代码。

#include<stdio.h>

void printFibonacci(int n){
    static int n1=0,n2=1,n3;
    if(n>0){
         n3 = n1 + n2;
         n1 = n2;
         n2 = n3;
         printf("%d ",n3);
         printFibonacci(n-1);
    }
}

int main()
{
    int n;
    char choice;
    printf("Enter the number of elements: ");
    scanf("%d",&n);
    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n-2);//n-2 because 2 numbers are already printed

    printf("\nDo you wish to continue?(Y/N)");
    scanf(" %c", &choice);

    while (choice=='Y')
    {
        printf("Enter the number of elements: ");
    scanf("%d",&n);
    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n-2);//n-2 because 2 numbers are already printed

    printf("\nDo you wish to continue?(Y/N)");
    scanf(" %c", &choice);
    }
  return 0;
 }


预期的结果是斐波那契数列。当我运行上面的代码时,如果我说 N = 5,它会给出正确的结果: 0,1,1,2,3。 在我对“您希望继续吗?(是/否)”回答是之后出现问题。如果我再次输入 N = 5,我希望得到 0,1,1,2,3,但我得到的是 0,1,5,8,13。

【问题讨论】:

  • 我认为你应该将 static var n1,n2,n3 重置为 0。

标签: c recursion fibonacci


【解决方案1】:

您的问题是使用这些静态变量。即使超出范围,它们也会保留它们的值,这意味着当调用函数时,n1 和 n2 都将具有它们最后分配的值。
我通过参数传递 n1 和 n2 解决了这个问题。这些函数取代了您的 printFibonacci。

void printFibonacci2(int length, int n1, int n2) {
    int n3;
    if(length > 0) {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        printf("%d ", n3);
        printFibonacci2(length - 1, n1, n2);
    }
}

void printFibonacci (int n)
{
  printFibonacci2(n, 0, 1);
}

你可以美化代码,但问题已经解决了。

【讨论】:

    【解决方案2】:

    我们可以通过在printFibonacci() 中添加else 大小写来保留您的函数签名,以便在n == 0 时重置其静态变量:

    #include <stdio.h>
    
    void printFibonacci(int n) {
        static int n1 = 0, n2 = 1, n3;
    
        if (n > 0) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            printf("%d ", n3);
            printFibonacci(n - 1);
        } else {
            n1 = 0;
            n2 = 1;
        }
    }
    
    int main()
    {
        int n;
        char choice = 'Y';
    
        while (choice == 'Y')
        {
            printf("Enter the number of elements: ");
            scanf("%d", &n);
            printf("Fibonacci Series: ");
            printf("%d %d ", 0, 1);
            printFibonacci(n - 2); // n - 2 because 2 numbers are already printed
            printf("\n");
    
            printf("Do you wish to continue? (Y/N): ");
            scanf(" %c", &choice);
        }
    
        return 0;
    }
    

    不是我的第一选择,而是解决混乱问题的最简单方法:

    > ./a.out
    Enter the number of elements: 10
    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 
    Do you wish to continue? (Y/N): Y
    Enter the number of elements: 10
    Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 
    Do you wish to continue? (Y/N): N
    >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      相关资源
      最近更新 更多