用递推比递归更加快
#include <iostream>
using namespace std;
typedef long long LL;
LL fact[25];
int main()
{
int n;
cin >> n;
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = i * fact[i-1];
cout << fact[n] << endl;
}
用递推比递归更加快
#include <iostream>
using namespace std;
typedef long long LL;
LL fact[25];
int main()
{
int n;
cin >> n;
fact[0] = 1;
for (int i = 1; i <= n; ++i)
fact[i] = i * fact[i-1];
cout << fact[n] << endl;
}