斐波那契 [英] FIbonacci

查看:86
本文介绍了斐波那契的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,伙计们这是斐波那契系列。但我有这个巨大的问题。

首先,我想做最简单的代码,然后我可以使用

用户定义的函数和数组。但是这个没出现。

任何建议!

#include< stdio.h>

#include< math.h>


int main()


{

int fib,n;


for(n = 0; n< = 10; n ++)

{


fib =(n-1)+(n-2) ;

printf("%d \ n",fib);


}


返回0 ;

}

解决方案

Am Sun,2005年12月11日07:06:00 -0800 schrieb MARQUITOS51 :

嘿伙计这是斐波那契系列。但我有这个巨大的问题。
首先,我想做最简单的代码,然后我可以使用
用户定义的函数和数组。但是这个没有出来。
任何建议!

#include< stdio.h>
#include< math.h>

int main()

int fib,n;

for(n = 0; n< = 10; n ++)
{

fib =(n-1)+(n-2);
printf("%d \ n",fib);

}

返回0;
}




据我记忆,这不是斐波纳契算法。

首先它以1和1开头,斐波纳契系列的第三个元素是第一个和第二个的增加。所以fib(n)= fib(n-1)+ fib(n-2)。

不保存所有元素,你只能保存最后的元素

变量并转移它们。但是你在索引元素上所做的并没有任何意义。


再见,

Stefan


嘿,我刚刚做了这个并且它有效。你能检查一下吗?

建议如何优化。我也在寻找如何将其转换为用户

定义的功能。


#include< stdio.h>

#include< math.h>


int main()


{int f [50];

int n = 2;


f [0] = 0;

f [1] = 1;


do

{

f [n] = f [n-1] + f [n-2];

n ++;

}

while(n< = 49);


for(n = 0; n< = 49; n ++)

{printf("%d \ n",f [n]);}

返回0;

}


MARQUITOS51< cr ******** @ gmail.com>写道:

嘿,我刚刚做了这个并且它有效。你能检查一下吗?
建议如何优化。我也在寻找如何将其转换为用户定义的功能。


当然,如果你没有计划想要超过50个b
斐波那契数字,它确实有效。 malloc()和朋友可以帮助您存储

任意数量的Fibonacci数字。如果你只是想打印它们,那么你不需要超过三个不同的变量。想想

吧。

#include< stdio.h>
#include< math.h>


为什么要包含此标题?你没有使用它。

int main()



int main(void)/ * better * /


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


Hey guys this is the fibonacci series. But Im having this huge problem.
First of all I want to do the simplest code possible so then I can use
user defined functions and arrays. But this one didnt came out well.
Any suggestions!
#include<stdio.h>
#include<math.h>

int main ()

{
int fib, n;

for(n=0; n<=10; n++)
{

fib=(n-1)+(n-2);
printf("%d \n", fib);

}

return 0;
}

解决方案

Am Sun, 11 Dec 2005 07:06:00 -0800 schrieb MARQUITOS51:

Hey guys this is the fibonacci series. But Im having this huge problem.
First of all I want to do the simplest code possible so then I can use
user defined functions and arrays. But this one didnt came out well.
Any suggestions!
#include<stdio.h>
#include<math.h>

int main ()

{
int fib, n;

for(n=0; n<=10; n++)
{

fib=(n-1)+(n-2);
printf("%d \n", fib);

}

return 0;
}



As far as I remember, this isn''t the fibonacci algorithm.
First it starts with 1 and 1, the third element of fibonacci series is the
addition of the first and second. So fib(n)=fib(n-1)+fib(n-2).
Not saving all elements you are able to only save the last elements in
variables and shift them. But what you did on the indexing element doesn''t
make any sense.

Bye,
Stefan


Hey I did this a few moments ago and it workes. Can you check it and
suggest how to optimize. Im also looking how to convert it to user
defined function.

#include<stdio.h>
#include<math.h>

int main ()

{ int f[50];
int n=2;

f[0]=0;
f[1]=1;

do
{
f[n]=f[n-1]+f[n-2];
n++;
}
while(n<=49);

for(n=0; n<=49; n++)
{printf("%d \n", f[n]);}
return 0;
}


MARQUITOS51 <cr********@gmail.com> wrote:

Hey I did this a few moments ago and it workes. Can you check it and
suggest how to optimize. Im also looking how to convert it to user
defined function.
Sure, it works, if you don''t ever plan on wanting more than 50
Fibonacci numbers. malloc() and friends can help you store an
arbitrary number of Fibonacci numbers. If you''re just trying to print
them, you don''t need more than three distinct variables. Think about
it.
#include<stdio.h>
#include<math.h>
Why are you including this header? You''re not using anything from it.
int main ()


int main( void ) /* better */

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于斐波那契的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆