#include<stdio.h>
//递归算法
int F(int n)
{
if(n == 0)
return n+1;
else
return n*F(n/2);
}
//消除递归
int F2(int n)
{
if(n>=0)
{
int outcome = 1;
int stack[100]; int top = -1; //建立并初始化一个栈
while(n>0)
{
stack[++top] = n;
n = n/2;
}
while(top != -1)
outcome *= stack[top--];
return outcome;
}
}
void main()
{
int n;
printf("请输入n的值:\n");
scanf("%d", &n);
printf("F(%d)=%d\n", n, F(n));
printf("F2(%d)=%d\n", n, F2(n));
}
3.25√ 试写出求递归函数F(n)的递归函数,并消除递归
最新推荐文章于 2024-05-15 21:33:13 发布