Dãy Fibonacci là dãy vô hạn các số tự nhiên bắt đầu bằng hai phần tử 1 và 1, các phần tử sau đó được thiết lập theo quy tắc mỗi phần tử luôn bằng tổng hai phần tử trước nó. Công thức truy hồi của dãy Fibonacci là:
// cbai14.cpp : Defines the entry point for the console application.
// code Dung NV
#include"stdafx.h"
#include<conio.h>
#include<stdio.h>
intfibonaxi(int n)
{
inta=1,b=2,tg,i=3;
if(n==1||n==2) return 1;
while(i<=n)
{
tg=a+b;
a=b;
b=tg;
i++;
}
returnb;
}
int_tmain(int argc, _TCHAR* argv[])
{
intn;
printf("\nNhap N= ");
scanf("%d",&n);
printf("so fibonaxi thu %5d la : %5d ",n,fibonaxi(n));
getch();
return0;
}
No comments:
Post a Comment