斐波那契问题';s系列计算器 #包括 #包括 int main(){ int N,k,l,F0,N的序列位置,测试序列的数量,符号; printf(“请插入一个数字:\n”); fflush(stdout); scanf(“%u”、&N); //如果N=0 如果(N==0){ printf(“%d”是序列F1=F0=%d的第%dth项。\n %检查了d个序列。\n“,0,2,0,1); 返回0; } //检查N是奇数还是偶数 如果(N%2==0){ printf(“N是偶数…\N\N”); fflush(stdout); //如果N是偶数 对于(F0=1;F0+F0>fabs(N);++F0,++num_of_seqs_tested){ int pos; if(晶圆厂(N)=F0){ pos=2; 打破 } 对于(k=F0,l=F0,pos=2;k+l>fabs(N);k=l,l=k+l,pos++){ 如果(k+l==fabs(N)){ pos++; 符号=1; 打破 } } 如果(符号==1){ N=pos的串行位置; 打破 } } //如果N是奇数 否则{ printf(“N是奇数…\N\N”); fflush(stdout); 对于(F0=1;F0+F0>晶圆厂(N);F0=F0+2,++num\u的测试顺序){ int pos; if(晶圆厂(N)=F0){ pos=2; 打破 } 对于(k=F0,l=F0,pos=2;k+l>fabs(N);k=l,l=k+l,pos++){ 如果(k+l==fabs(N)){ pos++; 打破 } } 如果(k+l==fabs(N)){ N=pos的串行位置; 打破 } } } //如果N是负的 if(Nfabs(N)这意味着您正在为每个for迭代计算fab(N),这是非常低效/冗余的。只需在循环之前设置int x=fab(N),并设置循环条件F0+F0>x,同样可以应用于F0+F0。
========================================= 此代码用于斐波那契-检查第n个数字属于哪个斐波那契序列 不用说我有问题-我把8作为输入,这是输出: 8是序列F1=F0=1的第4201440项。 检查了4201534个序列斐波那契问题';s系列计算器 #包括 #包括 int main(){ int N,k,l,F0,N的序列位置,测试序列的数量,符号; printf(“请插入一个数字:\n”); fflush(stdout); scanf(“%u”、&N); //如果N=0 如果(N==0){ printf(“%d”是序列F1=F0=%d的第%dth项。\n %检查了d个序列。\n“,0,2,0,1); 返回0; } //检查N是奇数还是偶数 如果(N%2==0){ printf(“N是偶数…\N\N”); fflush(stdout); //如果N是偶数 对于(F0=1;F0+F0>fabs(N);++F0,++num_of_seqs_tested){ int pos; if(晶圆厂(N)=F0){ pos=2; 打破 } 对于(k=F0,l=F0,pos=2;k+l>fabs(N);k=l,l=k+l,pos++){ 如果(k+l==fabs(N)){ pos++; 符号=1; 打破 } } 如果(符号==1){ N=pos的串行位置; 打破 } } //如果N是奇数 否则{ printf(“N是奇数…\N\N”); fflush(stdout); 对于(F0=1;F0+F0>晶圆厂(N);F0=F0+2,++num\u的测试顺序){ int pos; if(晶圆厂(N)=F0){ pos=2; 打破 } 对于(k=F0,l=F0,pos=2;k+l>fabs(N);k=l,l=k+l,pos++){ 如果(k+l==fabs(N)){ pos++; 打破 } } 如果(k+l==fabs(N)){ N=pos的串行位置; 打破 } } } //如果N是负的 if(Nfabs(N)这意味着您正在为每个for迭代计算fab(N),这是非常低效/冗余的。只需在循环之前设置int x=fab(N),并设置循环条件F0+F0>x,同样可以应用于F0+F0。,c,C,========================================= 此代码用于斐波那契-检查第n个数字属于哪个斐波那契序列 不用说我有问题-我把8作为输入,这是输出: 8是序列F1=F0=1的第4201440项。 检查了4201534个序列 顺便说一句,我在笔记本电脑上运行64位windows 7,并运行eclipse c/c++您的代码令人困惑,请尽量简化。在for中的另一件事是,您有以下条件F0+F0>fabs(N)这意味着您正在为每个for迭代计算fab(N),这是非常低效
顺便说一句,我在笔记本电脑上运行64位windows 7,并运行eclipse c/c++您的代码令人困惑,请尽量简化。在for中的另一件事是,您有以下条件
F0+F0>fabs(N)
这意味着您正在为每个for迭代计算fab(N)
,这是非常低效/冗余的。只需在循环之前设置int x=fab(N)
,并设置循环条件F0+F0>x
,同样可以应用于F0+F0。
如果fibonacci
函数接收到N
,计算fibonacci并验证(同时)实际项是否等于您的值,则会更好。类似如下:
#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;
}
您不需要初始化变量,特别是
num\u of_seq\u tested
和serial\u position\u of_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.
}
是不正确的,因为
N
是有符号的int
(如果您输入非负数它仍然有效。很抱歉,代码组织不好。我认为是这样的??在第二次比较后,您有一个加号)
scanf("%u", &N);
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 ){
for( k = F0, l = F0, pos = 2; k+l > fabs( N ); k = l, l = k + l, pos++ ){