题意:中文题。
分析:手动模拟加法和减法,注意一点细节就行了。
代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=10005;
const int MAX=1000000100;
const int mod=100000000;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=998244353;
const int INF=1000000010;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned long long ull;
char a[N],b[N];
int x[N],y[N],z[N];
void add() {
z[0]=max(x[0],y[0]);
for (int i=1;i<=z[0];i++) z[i]=x[i]+y[i];
for (int i=1;i<z[0];i++) z[i+1]+=z[i]/10,z[i]%=10;
while (z[z[0]]>9) {
z[z[0]+1]=z[z[0]]/10;
z[z[0]]%=10;z[0]++;
}
}
int pd() {
if (x[0]<y[0]) return 1;
if (x[0]>y[0]) return 0;
for (int i=x[0];i;i--)
if (x[i]!=y[i]) {
if (x[i]<y[i]) return 1;
else return 0;
}
return 2;
}
void sub() {
for (int i=1;i<=z[0];i++)
if (x[i]>=y[i]) z[i]=x[i]-y[i];
else { x[i+1]--;z[i]=x[i]+10-y[i]; }
while (z[0]>1&&z[z[0]]==0) z[0]--;
}
int main()
{
int i,ka=0,kb=0,lena,lenb;
scanf("%s", a);
scanf("%s", b);
lena=strlen(a);lenb=strlen(b);
if (a[0]=='-') ka=1;
if (b[0]=='-') kb=1;
if (ka==kb) {
for (i=lena-1;i>=ka;i--) x[++x[0]]=a[i]-'0';
for (i=lenb-1;i>=kb;i--) y[++y[0]]=b[i]-'0';
add();if (ka) printf("-");
} else {
for (i=lena-1;i>=ka;i--) x[++x[0]]=a[i]-'0';
for (i=lenb-1;i>=kb;i--) y[++y[0]]=b[i]-'0';
if (pd()) {
if (kb&&pd()!=2) printf("-");
ka=max(x[0],y[0]);
for (i=0;i<=ka;i++) swap(x[i],y[i]);
} else if (ka) printf("-");
z[0]=x[0];sub();
}
for (i=z[0];i;i--) printf("%d", z[i]);
printf("\n");
return 0;
}