2012-11-12 30 views
1
#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作爲輸入,這是輸入:

8是序列F1 = F0 = 1的4201440項。 4201534序列進行檢查。

地獄這是如何發生的?

順便說一句 - 我在我的電腦鐳拓運行windows 7 64位,並且運行Eclipse C/C++

感謝精彩的助手上帝保佑他們的靈魂

+0

對於糟糕的代碼組織,感到抱歉。我以爲是爲了那個? – galgal

+0

把晶圓廠的功能 – dreamcrash

+0

你有一個加號}第二次比較後 –

回答

0

你的代碼很混亂。嘗試編寫更簡單的代碼,它會幫助你。另一個想法是因爲你有以下條件F0 + F0> fabs(N),這意味着你對迭代fab(N)的每一個進行計算,高效低效/冗餘,只需在循環,並將其放在循環條件F0 + F0> x上,這同樣適用於F0 + F0。

我認爲你最好接受N,然後計算Fibonacci並同時驗證它的實際項是否等於你的值。

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. 
} 
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溢出)。