費波那其數列,1,1,2,3,5……編寫程序求第十項。可以用遞歸,也可以用其他方法,但要說明你選擇的理由。
#include <stdio.h>
int Pheponatch(int);
?
int main()
{
?? printf("The 10th is %d",Pheponatch(10));
?? return 0;
}
?
int Pheponatch(int N)
{
int a[10];//int a[N]不可以編譯器無法知道數組大小
a[0]=1;
a[1]=1;
for (int i=2;i<N;i++)
{
?a[i]=a[i-1]+a[i-2];
}
return a[N-1];
}
?
posted on 2006-08-29 11:10
創建更好的解決方案 閱讀(1719)
評論(6) 編輯 收藏 引用