33,010
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <stack>
using namespace std;
int fab(int n)
{
if (n <= 1)
return 1;
else
return n * fab(n - 1);
}
int main()
{
stack<int> records;
int n;
cin >> n;
records.push(n);
//栈顶为当前执行的函数,如果执行不了就把函数的信息压入栈,然后调用递归函数
while (records.top() != 1)
{
int newTop = records.top();
records.push(newTop - 1);
}
//退栈求结果
int result = 1;
while (!records.empty())
{
result *= records.top();
records.pop();
}
cout << result << endl;
return 0;
}