5 条题解
-
0
#include <iostream> #include <vector> using namespace std; // 计算阶乘的函数,返回存储阶乘结果的数组,低位在前 vector<int> factorial(int n) { vector<int> result(1, 1); // 初始化为1,低位在前,方便计算 for (int i = 2; i <= n; ++i) { int carry = 0; for (size_t j = 0; j < result.size(); ++j) { int product = result[j] * i + carry; result[j] = product % 10; carry = product / 10; } while (carry > 0) { result.push_back(carry % 10); carry /= 10; } } return result; } int main() { int n; cin >> n; if (n == 0 || n == 1) { cout << 1 << endl; return 0; } vector<int> res = factorial(n); // 从高位到低位输出结果 for (auto it = res.rbegin(); it != res.rend(); ++it) { cout << *it; } cout << endl; return 0; }
信息
- ID
- 1198
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 222
- 已通过
- 67
- 上传者