tanghg @ 2022-06-03 08:12:46
rt。
#include <iostream>
#include <cstring>
using namespace std;
const long long MAX=100;
typedef int ll;
struct Bigint{
ll l,a[MAX];
Bigint(ll x=0){
memset(a,0, sizeof(a));
for (l = 1; x ; ++l) {
a[l]=x%10;
x/=10;
}
l--;
}
int &operator[](ll i){
return a[i];
}
void fla(ll L){
l=L;
for (int i = 1; i <=l ; ++i) {
a[i+1]=a[i]/10;
a[i]%=10;
}
while (!a[l]){
l--;
}
}
void out(){
for (int i = max(1,l); i >=1 ; --i) {
cout<<a[i];
}
}
Bigint operator+(Bigint a,Bigint b){
Bigint c;
ll l= max(a.l,b.l);
for (int i = 1; i <=l ; ++i) {
c[i]+=a[i]+b[i];
}
c.fla(l+1);
return c;
}
Bigint operator*(Bigint a, ll b){
Bigint c;
ll l=a.l;
for (int i = 1; i <=l ; ++i) {
c[i]=a[i]*b;
}
c.fla(l+11);
return c;
}
};
int main() {
Bigint a(0),f(1);
ll n;
cin>>n;
for (int i = 1; i <=n ; ++i) {
f= f * i;
a= a + f;
}
a.out();
return 0;
}
还有一个小问题。
Bigint(ll x=0){
memset(a,0, sizeof(a));
for (l = 1; x ; ++l) {
a[l]=x%10;
x/=10;
}
l--;
}
这一段代码有什么作用呢,为什么要这样初始化? 第一次发帖,有什么问题我会改。
by tanghg @ 2022-06-03 10:38:46
此帖结