斐波那契系列计算器的问题

时间:2012-11-12 00:47:16

标签: c

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

int main(){

    int N,  k, l, F0,serial_position_of_N,num_of_seqs_tested,sign;

    printf("please insert a number:\n");
    fflush(stdout);
    scanf("%u", &N);

    //if N=0
    if( N == 0 ){
        printf("%d is the %dth item of the sequence F1 = F0 = %d.\n
            %d sequences were checked.\n", 0, 2, 0, 1);
            return 0;
    }

    //checks if N is odd or even
    if( N % 2 == 0 ){
        printf("N is and even number....\n\n");
        fflush(stdout);

      //if N is even
      for( F0 = 1; F0 + F0 > fabs( N ); ++F0, ++num_of_seqs_tested ){
          int pos;

          if( fabs( N ) == F0 ){
              pos = 2;
              break;
          }

          for( k = F0, l = F0, pos = 2; k+l > fabs( N ); k = l, l = k + l, pos++ ){
              if(k+l == fabs(N)){
          pos++;
          sign = 1;
          break;
          }
      }

      if( sign == 1 ){
          serial_position_of_N = pos;
          break;
      }
    }

    //if N is Odd
    else{
        printf( "N is and odd number....\n\n" );
        fflush( stdout );
        for( F0 = 1; F0 + F0 > fabs( N ); F0= F0 + 2, ++num_of_seqs_tested){
            int pos;

        if( fabs( N ) == F0 ){
            pos = 2;
            break;
        }

        for( k = F0, l = F0, pos = 2; k+l>fabs(N); k = l, l = k+l, pos++ ){

            if( k + l == fabs( N ) ){
                pos++;
                break;
            }

        }


        if( k+l == fabs( N ) ){
            serial_position_of_N = pos;
            break;
            }
        }
    }

//if N is negetive
    if( N < 0 ){
        printf("%d is the %dth item of the sequence F1 = F0 = %d.\n%d sequences were checked.\n", N, serial_position_of_N, (-1)*F0, num_of_seqs_tested);
        fflush(stdout);
    }

    //other
    printf( "%d is the %dth item of the sequence F1 = F0 = %d.\n%d sequences were checked.\n", N, serial_position_of_N, F0, num_of_seqs_tested );
    fflush(stdout);
    return 0;
}

=========================================

此代码用于斐波纳契 - 检查N编号属于哪个Fibonnaci序列。 不用说我有问题 - 我把8作为输入,这是ouptut:

8是序列F1 = F0 = 1的4201440项。 检查了4201534个序列。

这到底是怎么发生的......

BTW - 我在我的latop pc上运行Windows 7 64位,然后运行eclipse c / c ++

感谢上帝保佑他们灵魂的美妙帮助

2 个答案:

答案 0 :(得分:1)

您没有初始化变量,特别是num_of_seqs_testedserial_position_of_N未经初始化而使用。

scanf("%u", &N);

不正确,因为N是签名的int(如果您输入非负数<= INT_MAX,它仍然有效)。如果输入8,则将执行代码路径

if( N % 2 == 0 ){
    printf("N is and even number....\n\n");
    fflush(stdout);

  //if N is even
  for( F0 = 1; F0 + F0 > fabs( N ); ++F0, ++num_of_seqs_tested ){

1 + 1 < 8起,循环永远不会运行。您可能打算使用<代替>

但请注意

for( k = F0, l = F0, pos = 2; k+l > fabs( N ); k = l, l = k + l, pos++ ){

不会产生类似Fibonacci序列的任何内容,在循环中第一次更新后,您将拥有不变l = 2*k(直到l溢出)。

答案 1 :(得分:0)

您的代码令人困惑,请尽量使其更简单。 for 中的另一件事,您有以下条件F0+F0>fabs(N)这意味着您正在计算每个 for 迭代fab(N),这是非常低效的/冗余。只需在循环前设置int x = fab(N),然后设置循环条件F0+F0 > x,同样可以应用于F0+F0.

如果fibonacci函数收到N,计算斐波纳契并验证(同时)实际项是否等于您的值,那会更好。像这样:

int fibonacci(int n)
{
  int a = 0,b = 1, sum;
  int n_th = 1;
  int find = 0;
  int erro = 0; 
  while(!find && !erro)         // Search until it is find or a erro occurs.
  {
    if(a == n) find = 1;           // we find it;
    else if ( a > n) erro = 1;     // this means that N do not belong to fibonacci
    else n_th++;                   // increment the position of the term
    sum = a + b;                   // sum = the previous 2 terms
    a = b;                         // a takes the value of the actual term
    b = sum;                       // b becomes the next term
  }
  if(erro) return 0;               // Means that N do not belong to fibonacci.
  else return n_th;                // Gives the position.
}