wbs123456 @ 2023-05-10 19:48:36
以下是C++高精度代码:
#include <iostream>
#include <vector>
using namespace std;
const int BASE = 100000000;
const int WIDTH = 8;
vector<int> operator+(const vector<int>& a, const vector<int>& b) {
vector<int> c;
int t = 0;
for (int i = 0; i < a.size() || i < b.size(); i++) {
if (i < a.size()) t += a[i];
if (i < b.size()) t += b[i];
c.push_back(t % BASE);
t /= BASE;
}
if (t) c.push_back(1);
return c;
}
vector<int> operator*(const vector<int>& a, int b) {
vector<int> c;
int t = 0;
for (int i = 0; i < a.size(); i++) {
t += a[i] * b;
c.push_back(t % BASE);
t /= BASE;
}
while (t) {
c.push_back(t % BASE);
t /= BASE;
}
return c;
}
void print(const vector<int>& a) {
printf("%d", a.back());
for (int i = a.size() - 2; i >= 0; i--) {
printf("%08d", a[i]);
}
}
int main() {
int n;
cin >> n;
vector<int> s(1, 1);
vector<int> factorial(1, 1);
for (int i = 2; i <= n; i++) {
factorial = factorial * i;
s = s + factorial;
}
print(s);
return 0;
}
以前不会CPP高精打表水过,现在用vector重载了+和*运算符,为什么75分,#4输出了一连串负数?