斐波那契数列:我的代码出了点问题,结果输出?

时间:2019-10-29 17:40:06

标签: c do-while fibonacci stdio

第一个输入为5,第二个输入13为输出,假设为5、8,13

#include<stdio.h>

int main() {
    int lim_up, lim_low, A=5, B=13, C=8;

    printf("\n\n\t ENTER THE LOWER LIMIT: ");
    scanf("%d", &lim_low);
    printf("\n\n\t ENTER THE UPPER LIMIT: ");
    scanf("%d", &lim_up);
    printf("\n\n\t FIBONACCI NUMBERS ARE: ");


    do{
         lim_up++;

         printf("\n\n\t\t\t%d", A);
         A = C+B;
         B = c;
         C= A;
    }while(A<lim_up);
    getch();
}

我希望输出为5 8 13

2 个答案:

答案 0 :(得分:2)

以下代码在间隔 [lim_low ... lim_up] 中打印斐波那契序列:

#include<stdio.h>

int main(void)
{
    unsigned int lim_up, lim_low;
    printf("ENTER THE LOWER LIMIT: ");
    scanf("%u", &lim_low);
    printf("ENTER THE UPPER LIMIT: ");
    scanf("%u", &lim_up);
    printf("FIBONACCI NUMBERS ARE: ");

    unsigned int t1 = 0, t2 = 1, nextTerm = 0;

    while(nextTerm <= lim_up){
        if (nextTerm >= lim_low)
            printf("%u ",  nextTerm);

        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
    return 0;
}

我们必须计算所有斐波那契系列,当我们达到我们感兴趣的间隔时,我们便开始打印结果。

注意:这不是最有效的方式。
O(1) mathematical formula可以做到这一点。立即没有循环。

附录:以数学方式进行
这是直接计算 F(n)的代码:

unsigned fib(unsigned int n) { 
  double phi = (1 + sqrt(5)) / 2; 
  return round(pow(phi, n) / sqrt(5)); 
} 

现在要从 F(n)获得 n ,您将需要以下代码:

unsigned reverseFib(unsigned int fn) { 
  double phi = (1 + sqrt(5)) / 2; 
  return round(log(sqrt(5) * fn) / log(phi)); 
} 

请注意:

  1. 上面的函数将适用于所有期望 fn = 1 的斐波那契数。
    为什么它对 fn = 1 不起作用?因为 fn = 1 可以被推导为1或2,所以由于有两种可能性(数学上: F(n) 不是[1、2]中 n bijective function
  2. 理论上可能会发生浮点错误,因为我们的计算机没有无限的精度,但是这种情况很少发生。
  3. 我测试了前12个斐波那契数字的功能, 工作正常,但 fn = 2 除外,如上所述 点 1

答案 1 :(得分:0)

斐波那契以两个数字开头。

unsigned previous = 0;
unsigned current  = 1;

下一项由和给出。

unsigned next = previous + current;

那我们就可以转移一切。

previous = current;
current  = next;

现在,您只需要引入一个循环,并检查是否应该打印或退出循环。

#include <stdio.h>

/* **********

Fibonacci:
  F[0] = 0
  F[1] = 1
  F[n] = F[n-2] + F[n-1]

********** */

int main(void) {
   unsigned min = 5;
   unsigned max = 13;

   unsigned previous = 0;
   if (previous >= min)
      print("%u\n", current);

   unsigned current = 1;
   while (current <= max) {
      if (current >= min)
         printf("%u\n", current);

      unsigned next = previous + current;
      previous = current;
      current  = next;
   }

   return 0;
}

但是有一个更简单的解决方案。

#include <stdio.h>

/* **********

Fibonacci:
  F[0] = 0
  F[1] = 1
  F[n] = F[n-2] + F[n-1]

If we don't print the first element, this is the same as
  F[0] = 1
  F[1] = 0
  F[n] = F[n-2] + F[n-1]

Taking this approach simplifies the code. 

********** */

int main(void) {
   unsigned min = 5;
   unsigned max = 13;

   unsigned previous = 1;
   unsigned current  = 0;
   while (current <= max) {
      if (current >= min)
         printf("%u\n", current);

      unsigned next = previous + current;
      previous = current;
      current  = next;
   }

   return 0;
}

最后是使用@Omarito提供的功能的优化解决方案。

#include <math.h>
#include <stdio.h>

const double phi = (1 + sqrt(5)) / 2;

unsigned fib(unsigned n) {
   return round(pow(phi, n) / sqrt(5));
}

unsigned reverse_fib(unsigned fn) {
   return round(log(sqrt(5) * fn) / log(phi));
}

int main(void) {
   unsigned min = 5;
   unsigned max = 13;

   unsigned previous;
   unsigned current;
   if (min == 0) {
      previous = 1;
      current  = 0;
   }
   else if (min == 1) {
      previous = 0;
      current  = 1;
   }
   else {
      current  = min;
      previous = fib(reverse_fib(current)-1);
   }

   while (current <= max) {
      printf("%u\n", current);

      unsigned next = previous + current;
      previous = current;
      current  = next;
   }

   return 0;
}