計算費布納西數列第n項及其總和?(遞迴函式)
(1,1,2,3,5,8,13,21,34,55......)
#include
int F(int n){
if(n==1 || n==2)
return 1;
else
return F(n-1)+F(n-2);
}
int main(void) {
//輸入費數第n項求其值及總和?
int i,n,sum;
sum = 0;
scanf("%d",&n);
printf("第%d項為:%d\t總和為:",n,F(n));
for (i=1;i
{
sum = sum +F(i);
printf("%d+",F(i));
}
printf("%d=%d",F(n),sum+F(n));
return 0;
}
0 意見:
張貼留言