#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++