C语言递归
来源:百度知道 编辑:UC知道 时间:2024/06/25 01:28:21
1,2,2,4,8,32,256,
按照上面数字的规律,输出第八项的值,要求用递归~!~!
按照上面数字的规律,输出第八项的值,要求用递归~!~!
#include <stdio.h>
int fun(int a)
{
if(a < 1) return 0;
if(a == 1) return 1;
if(a == 2) return 2;
return fun(a-1) * fun(a - 2);
}
int main()
{
printf("fun(8) = %d\n", fun(8));
return 0;
}
int main(){
int i = 1;
int j = 2;
int temp;
int cnt = 1;
while(cnt<=7){
cnt++;
temp = i * j;
i = j;
j = temp;
}
printf("%d",j)
return 0;
}