2
#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 个数字属于哪个斐波那契数列。

不用说我有问题 - 我把 8 作为输入,这是输出:

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

顺便说一句 - 我在笔记本电脑上运行 windows 7 64 位,并运行 eclipse c/c++

4

2 回答 2

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++ ){

不会产生任何类似于斐波那契数列的东西,在循环中的第一次更新之后,您将拥有不变量l = 2*k(直到l溢出)。

于 2012-11-12T12:41:37.283 回答
0

您的代码令人困惑,请尝试使其更简单。你的另一件事是你有以下条件F0+F0>fabs(N),这意味着你正在为每个迭代计算fab(N),这是非常低效/冗余的。只需int x = fab(N)在循环之前 make ,并加上循环条件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.
}
于 2012-11-12T01:13:38.150 回答