Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
168673 | 赖子洋 | 高精度阶乘 | C++ | 解答错误 | 0 | 1000 MS | 468 KB | 939 | 2024-08-20 13:05:22 |
#include <iostream> #include <string> #include <algorithm> std::string multiply(const std::string &a, const std::string &b) { std::string result(a.size() + b.size(), '0'); for (int i = a.size() - 1; i >= 0; --i) { for (int j = b.size() - 1; j >= 0; --j) { int carry = (result[i + j + 1] - '0') + (a[i] - '0') * (b[j] - '0'); result[i + j + 1] = (carry % 10) + '0'; result[i + j] += carry / 10; } } result.erase(result.begin(), std::find_if(result.begin(), result.end(), [](char c) { return c != '0'; })); return result[0] == '\0' ? "0" : result; } std::string factorial(int n) { std::string product = "1"; for (int i = 2; i <= n; ++i) { product = multiply(product, std::to_string(i)); } return product; } int main() { int n; std::cin >> n; std::cout << factorial(n) << std::endl; return 0; }