关于婓波那契数列

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:17:34
要求写一个C程序,产生婓波那契数列的前10个数。我自己的方法是死做,这样没什么效率。循环不知道怎么写好。。规律自己找到了。

int a,b,num1,num2,num3,num4;

a=1,b=1,num1=a+b,num2=b+num1,num3=num2+num1,num4=num3+num2;

printf("%d\n",num1);
printf("%d\n",num2);
printf("%d\n",num3);
printf("%d\n",num4);
望高手点拨。
产生的程序是 1,1,2,3,5,8,13,...........最好仅限于 while ,do while,for,,数组我们没教到

#define N 10//Chenge this Number to have different result
#include<stdio.h>

void main()
{
int i,a,b,c;//Array to mem

a=1;
b=1;
printf("第%d个数是:%d\n",1,a);
printf("第%d个数是:%d\n",2,b);
for(i=3;i<=N;i++)
{
c=a+b;
printf("第%d个数是:%d\n",i,c);
b=c;
a=b;
}
}
调试了,对的,第一句话最后的数是婓波那契数列的个数

传说中的兔子数列
1,1,2,3,5,8,13......
从第三项开始,每一项都等于前面两项的和

//VC++6.0下调试通过
#include <iostream>
using namespace std;

int fbn(int n)
{
int result=0;
switch (n)
{
case 1:
case 2:
return 1;
default:
return fbn(n-1)+fbn(n-2);
}
}

void main()
{
for (int i=1; i<=10; i++)
{
cout<<"n=1 时 "<<fbn(i)<<endl;
}
}