不习惯写java,还是留个大数模板以后用吧。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define base 10000
//10000进制
struct bign{
int l,a[M];
bign(){
memset(a,0,sizeof(a)); l = 0;
}
bign(int x){
memset(a,0,sizeof(a));
for(l= 0; x; x/= base)
a[l++] = x%base;
if(!l) l++;
}
bign operator = (bign x){
memcpy(a,x.a,sizeof(a));
l = x.l;
return *this;
}
bign operator + (bign x){
bign res; int i,cy = 0;
for(i = 0; i<l||i<x.l||cy > 0; i++){
if(i < l) cy += a[i];
if(i < x.l) cy += x.a[i];
res.a[i] = cy %base;
cy /= base;
}
res.l = i;
return res;
}
bign operator *(bign x){
bign res; res.l = 0;
if(0 == x.l){
res.a[0] = 0; x.l = 1; return res;
}
for(int i = 0; i < l; i++)
for(int j = 0,cy = 0; j <x.l||cy>0; j++,cy/=base){
if(j < x.l) cy += a[i]*x.a[j];
if(i+j < res.l) cy += res.a[i+j];
if(i+j >= res.l)
res.a[res.l++] = cy%base;
else res.a[i+j] = cy%base;
}
return res;
}
void print(){
int i;
printf("%d",l==0?0:a[l-1]);
for(i = l-2; i >= 0; i--)
printf("%04d",a[i]);
printf("\n");
}
};