酷代码 AI
菜单

求 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>n</mi> <mo>!</mo> <mo stretchy="false">(</mo> <mn>0</mn> <mo>&lt;=</mo> <mi>n</mi> <mo>&#x2264;</mo> <mn>12</mn> <mo stretchy="false">)</mo> </math> ,也就是 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>n</mi> <mo>!</mo> <mo stretchy="false">(</mo> <mn>0</mn> <mo>&lt;=</mo> <mi>n</mi> <mo>&#x2264;</mo> <mn>12</mn> <mo stretchy="false">)</mo> </math> 。 阶乘可以用如下递归式描述: <math xmlns="http://www.w3.o

```cpp #include <iostream> using namespace std; int factorial(int n) { if (n == 0) return 1; // 0! = 1 return n * factorial(n - 1); // 递归计算阶乘 } int main() { int n; cin >> n; cout << factorial(n) << endl; return 0; } ``` 输入 `3` 时,程序计算 `3! = 3 × 2 × 1 = 6`,输出 `6`。 [2026-01-05 17:44:02 | AI问答 | 117点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多