Run ID 作者 问题 语言 测评结果 Time Memory 代码长度 提交时间
141061 胡海峰老师 n的阶乘 C++ Accepted 0 MS 272 KB 454 2025-12-20 21:36:45

Tests(5/5):


Code:

#include <iostream> using namespace std; // 递归函数计算阶乘 int factorial(int n) { // 基本情况:0的阶乘和1的阶乘都是1 if (n == 0 || n == 1) { return 1; } // 递归情况:n! = n * (n-1)! else { return n * factorial(n - 1); } } int main() { int n; cin >> n; // 输入n // 计算并输出阶乘 cout << factorial(n) << endl; return 0; }