1009 B
Raw Blame History

1、求N的值 【问题描述】

用高精度方法求N的精确值(N以一般整数输入)。 【输入样例】ni.in

10

【输出样例】ni.out

3628800
#include<iostream>
#include<string>
#include<vector>
using namespace std;

// 高精度字符串乘法res * num
string multiply(string res, int num) {
    vector<int> temp;
    int carry = 0;
    for (int i = res.size() - 1; i >= 0; i--) {
        int product = (res[i] - '0') * num + carry;
        temp.push_back(product % 10);
        carry = product / 10;
    }
    while (carry) {
        temp.push_back(carry % 10);
        carry /= 10;
    }

    // 把结果翻转回来
    string result;
    for (int i = temp.size() - 1; i >= 0; i--) {
        result += temp[i] + '0';
    }
    return result;
}

int main() {
    int n;
    cin >> n;
    string result = "1";

    for (int i = 2; i <= n; i++) {
        result = multiply(result, i);
    }
    cout << result << endl;
    return 0;
}