52 lines
1009 B
Markdown
52 lines
1009 B
Markdown
## 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);
|
||
}
|
||
cout << result << endl;
|
||
return 0;
|
||
}
|
||
|
||
``` |