高精度加法
string add(string x,string y){
int a[1010] = {},b[1010] = {},lenx = x.size(),leny = y.size();
for(int i = lenx - 1 ; i >= 0 ; i --)a[i] = x[lenx - i - 1] - '0';
for(int i = leny - 1 ; i >= 0 ; i --)b[i] = y[leny - i - 1] - '0';
int len = max(lenx,leny);
for(int i = 0 ; i < len ; i ++){
a[i] += b[i];
a[i + 1] += a[i] / 10;
a[i] %= 10;
}
if(a[len])len ++;
string res = "";
for(int i = len - 1; i >= 0 ; i --)res += a[i] + '0';
if(res.size() == 0)return "0";
return res;
}
高精度乘法
string mul(string x,string y){
int a[2010] = {},b[2010] = {},c[4010] = {},lenx = x.size(),leny = y.size();
for(int i = 0 ; i < lenx ; i ++)a[i] = x[lenx - i - 1] - '0';
for(int j = 0 ; j < leny ; j ++)b[j] = y[leny - j - 1] - '0';
for(int i = 0 ; i < lenx ; i ++){
for(int j = 0 ; j < leny ;j ++){
c[i + j] += a[i] * b[j];
c[i + j + 1] += c[i + j] / 10;
c[i + j] %= 10;
}
}
int len = lenx + leny;
while(len > 1 && c[len - 1] == 0)len -- ;
string res = "";
for(int i = len - 1; i >= 0 ;i --)res += c[i] + '0';
return res;
}