给你
a
,
b
a,b
a,b 要你求
a
+
b
a+b
a+b
其中
−
1
0
30
<
a
,
b
<
1
0
30
-10^{30}<a,b<10^{30}
−1030<a,b<1030 且
a
∗
b
>
=
0
a*b>=0
a∗b>=0
解
高精加即可。【但是这里也打了高精减】
代码
#include<cstdio>
char c;
int f[5] = {0,1,1}, a[5][50], len[5], dx, qu;
void read(int k){
c = getchar();
while((c > '9' || c < '0') && c != '-') c = getchar();
if(c == '-'){
f[k] = -1;
c = getchar();
}
while(c <= '9' && c >='0'){
a[k][++len[k]] = c - 48;
c = getchar();
}
}
int db(){
if(len[1] > len[2]) return 1;
if(len[2] > len[1]) return 2;
for(int i = 1; i <= len[1]; ++i){
if(a[1][i] > a[2][i]) return 1;
if(a[1][i] < a[2][i]) return 2;
}
return 3;
}
void add(){
int jw = 0, flag = 0;
for(int i = 40; i; --i){
a[1][i] = a[1][i] + a[2][i] + jw;
jw = a[1][i] / 10;
a[1][i] = a[1][i] % 10;
}
for(int i = 1; i <= 40; ++i){
if(a[1][i] != 0) flag = 1;
if(flag == 1) printf("%d", a[1][i]);
}
}
void cut(int aa, int bb){
int jw = 0, flag = 0;
for(int i = 40; i; --i){
a[aa][i] = a[aa][i] - a[bb][i];
if(a[aa][i] < 0){
a[aa][i] += 10;
--a[aa][i-1];
}
}
for(int i = 1; i <= 40; ++i){
if(a[aa][i] != 0) flag = 1;
if(flag == 1) printf("%d", a[aa][i]);
}
}
int main(){
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
read(1);
read(2);
dx = db();
qu = 40-len[1];
for(int i = 40; i >= 1; --i){
if(i-qu >= 1) a[1][i] = a[1][i-qu];
else a[1][i] = 0;
}
qu = 40-len[2];
for(int i = 40; i >= 1; --i){
if(i-qu >= 1) a[2][i] = a[2][i-qu];
else a[2][i] = 0;
}
if(f[1] == 1 && f[2] == 1)
add();
else
if(f[1] == -1 && f[2] == -1){
printf("-");
add();
}
else
if(dx == 3){
printf("0");
}
else if(dx == 1){
if(f[1] == -1) printf("-");
cut(1, 2);
}
else{
if(f[2] == -1) printf("-");
cut(2, 1);
}
fclose(stdin);
fclose(stdout);
}