今天鞋百科给各位分享斐波那契饿数列前十项的知识,其中也会对用递归法求斐波那契数列前40项。(利用递归函数 求出斐波那契 数列 的前20项)进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在我们开始吧!

用递归法求斐波那契数列前40项。

用递归法求斐波那契数列前40项方法为:

1、首先,对非法下标进行判断。

2、定义出递归调用的出口n=1或n=2,直接返回1。

用递归法求斐波那契数列前40项。

3、使用递归直接调用自身即可,不需要使用数组存储,而是使用压入栈 的数据。注意idea中侧边会显示递归的小圈。

4、添加测试函数,输出前5项与前10项。

5、测试结果如下。

注意事项:

斐波那契数列在自然科学的其他分支,有许多应用。例如,树木的生长,由于新生的枝条,往往需要一段“休息”时间,供自身生长,而后才能萌发新枝。

编写一个C程序,用于产生斐波那契数列的前10个数。

很简单
#include
main()
{int a=1,b=1,c,s=1;
do{
c=a+b;
printf("%d,",a);
a=b;
b=c;
s++;
}
while(s<=10);
}

现在对了吧,试着运行一下。

C语言编程:用递归和非递归法输出斐波那契数列

你用的什么编译器 我用VC++6.0完全正常 我给你贴图

循环版&nbsp

=========================================&nbsp

#include&nbsp &nbsp &nbsp

int&nbsp &nbsp main()&nbsp

{&nbsp

&nbsp &nbsp &nbsp &nbsp unsigned&nbsp &nbsp int&nbsp &nbsp a[40]&nbsp &nbsp =&nbsp &nbsp {0,&nbsp &nbsp 1};&nbsp

&nbsp &nbsp &nbsp &nbsp printf&nbsp &nbsp ("%d\n%d\n",&nbsp &nbsp a[0],&nbsp &nbsp a[1]);&nbsp

&nbsp &nbsp &nbsp &nbsp for&nbsp &nbsp (&nbsp &nbsp int&nbsp &nbsp i&nbsp &nbsp =&nbsp &nbsp 2;&nbsp &nbsp i&nbsp &nbsp <&nbsp &nbsp 40;&nbsp &nbsp ++i)&nbsp

&nbsp &nbsp &nbsp &nbsp {&nbsp

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp a[i]&nbsp &nbsp =&nbsp &nbsp a[i-1]&nbsp &nbsp +&nbsp &nbsp a[i-2];&nbsp

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp printf&nbsp &nbsp ("%d\n",&nbsp &nbsp a[i]);&nbsp

&nbsp &nbsp &nbsp &nbsp }&nbsp

&nbsp &nbsp &nbsp &nbsp return&nbsp &nbsp 0;&nbsp

}&nbsp

===========================================&nbsp

递归版&nbsp

===========================================&nbsp

#include&nbsp &nbsp &nbsp

int&nbsp &nbsp fb&nbsp &nbsp (int&nbsp &nbsp i)&nbsp

{&nbsp

&nbsp &nbsp &nbsp &nbsp if&nbsp &nbsp (&nbsp &nbsp i&nbsp &nbsp <&nbsp &nbsp 2&nbsp &nbsp )&nbsp

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp return&nbsp &nbsp i&nbsp &nbsp ==&nbsp &nbsp 0&nbsp &nbsp ?&nbsp &nbsp 0&nbsp &nbsp :&nbsp &nbsp 1;&nbsp

&nbsp &nbsp &nbsp &nbsp return&nbsp &nbsp fb&nbsp &nbsp (i&nbsp &nbsp -&nbsp &nbsp 1)&nbsp &nbsp +&nbsp &nbsp fb&nbsp &nbsp (i&nbsp &nbsp -&nbsp &nbsp 2);&nbsp

}&nbsp

int&nbsp &nbsp main()&nbsp

{&nbsp

&nbsp &nbsp &nbsp &nbsp for&nbsp &nbsp (&nbsp &nbsp int&nbsp &nbsp i&nbsp &nbsp =&nbsp &nbsp 0;&nbsp &nbsp i&nbsp &nbsp <&nbsp &nbsp 40;&nbsp &nbsp ++i)&nbsp

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp printf&nbsp &nbsp ("%d\n",&nbsp &nbsp fb&nbsp &nbsp (i));&nbsp

&nbsp &nbsp &nbsp &nbsp return&nbsp &nbsp 0;&nbsp

}&nbsp

请问斐波那契数列的第9项是多少?

34

斐波那契数列(Fibonaccisequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(LeonardodaFibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)

用一维数组求斐波那契数列前10项

#include
int main(){
int i;
int fibo[10];
fibo[0]=1;fibo[1]=1;
for(i=2;i<10;i++){
fibo[i]=fibo[i-1]+fibo[i-2];
}//所有的数都存在了fibo数组里

//在这里你可以打印或是干什么其他事情了
return 0;
}

用c语言编写,输出斐波那契数列的前10个数

#include
main()
{int a=1,b=1,c,s=1;
do{
c=a+b;
printf("%d,",a);
a=b;
b=c;
s++;
}
while(s<=10);
}

VB将斐波那契数列的前10项写入文件FB.DAT,然后从该文件将数据读取出来并计算合计

Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim n(9) As Integer
n(0) = 0
n(1) = 1
For i = 2 To 9
n(i) = n(i - 1) + n(i - 2)
Next
Open App.Path & "\FB.dat" For Output As #1
For i = 0 To 9
Write #1, "fib(" & i & ")", n(i)
Next
Close #1
End Sub

Private Sub Command2_Click()
Dim n(9) As Integer
Dim j() As String
i = 0
Open App.Path & "\FB.dat" For Input As #1
Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, textline ' 读入一行数据并将其赋予某变量。
j = Split(textline, ",")
n(i) = j(1)
i = i + 1
List1.AddItem textline
Loop
Close #1
For i = 0 To 9
Sum = Sum + n(i)
Next
List1.AddItem "合计: " & Sum
List1.AddItem "平均: " & Sum / 10

计算裴波那契数列的前二十个数,并以每行五个输出

斐波那契数列只要根据定义,一步一步求数就行了。

具体程序如下:

void Fibonacci(int n){ int arr[100] = {0, 1, 1 }; for (int i = 3; i <= n; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } for (int i = 1; i <= n; i++) { cout << arr[i] << " "; if (i % 5 == 0) cout << endl; } cout << endl;}结果如图:

c语言编程题(用一维数组二维数组的方法做) (1)用一堆数组计算Fibonacci数列的前20项。F

思路:先定义数组的前两项值,再依次对后面每项进行赋值,后面每项都是前两项的和。

例如:

#include int main(){

int a[20],i;

a[0]=a[1]=1;

for(i=2;i<20;i++)

a[i]=a[i-1]+a[i-2];

for(i=0;i<20;i++){

printf("%4d ",a[i]);

if((i+1)%5==0)

printf("\n");

}

return 0;}/*输出: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765*/