53 lines
1.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 1、求N的值 【问题描述】
用高精度方法求N的精确值(N以一般整数输入)。
【输入样例】ni.in
```in
10
```
【输出样例】ni.out
```out
3628800
```
```cpp
#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);
}
reverse(result.begin(), result.end());
cout << result << endl;
return 0;
}
```