3.25√ 试写出求递归函数F(n)的递归函数,并消除递归
作者:互联网
#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));
}
标签:F2,return,递归,递归函数,int,top,3.25,printf,stack 来源: https://blog.csdn.net/qq_43661234/article/details/101064609