法一〉#include
using namespace std;
long long factorial(int n)
{
int sum = 1;
for(int i = 1; i <= n; i++)
sum = sum * i;
return sum;
}
int main() {
int n;
cin >> n;
cout <<factorial(n) << endl;
return 0;
}
法二〉#include
using namespace std;
long long recursion(int n){
if(n == 1)
return 1;
return recursion(n - 1) * n; //递归计算n*f(n-1)
}
int main() {
int n;
cin >> n;
long long factorial = recursion(n); //递归
cout << factorial << endl;
return 0;
}