题意如下:
任意输入两个正整数n, m(0 <= n, m <= 10^100),求出n-m;
模板代码如下:
#include <stdio.h>
#include <string.h>
#define MAXN 10005
#define clr(N)memset(N, 0, sizeof(N))
int A[MAXN], B[MAXN], ans[MAXN];
char a[MAXN], b[MAXN];
void Sub(char p[], char q[], int Lp, int Lq)
{
clr(A), clr(B), clr(ans);
int k = 0;
for(int i=Lp-1; i>=0; i--) {
A[k++] = p[i] - 48;
}
k = 0;
for(int i=Lq-1; i>=0; i--) {
B[k++] = q[i] - 48;
}
for(int i=0; i<MAXN; i++) {
ans[i] = A[i] - B[i];
if(ans[i] < 0) {
ans[i] = ans[i] + 10;
A[i+1] = A[i+1] - 1;
}
}
}
int main() {
while(~scanf("%s%s", a, b)) {
if(strcmp(a, b)==0) {
printf("0\n");
continue;
}
int La = strlen(a);
int Lb = strlen(b);
int FG = 0;
if(La < Lb || (La == Lb && strcmp(a, b) < 0)) {
Sub(b, a, Lb, La);
FG = 1;
}else {
Sub(a, b, La, Lb);
}
if(FG) printf("-");
int flag = 0;
for(int i=MAXN-1; i>=0; i--) {
if(flag) {
printf("%d", ans[i]);
continue;
}else if(ans[i]) {
flag = 1;
printf("%d", ans[i]);
}
}
printf("\n");
}
}