#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 シーケンスがチェックされました。
ところで-ラップトップPCでWindows 7 64ビットを実行し、eclipse c/c ++を実行します