该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
只写过加法的,杭电的A + B Problem (II)(AC):
#include
#include
#include
#define N 3000
int main(void)
{
int n, i, j, k, p, q, a, b, t, up;
char s[N];
scanf("%d ", &n);
for (i = 0; i < n; i++) {
int sum[N] = {0};
printf("Case %d:\n", i + 1);
fgets(s, N, stdin);
for (t = q = strlen(s) - 2; isdigit(s[t]); t--)
;
for (p = t - 1, up = j = 0; p >= 0 || isdigit(s[q]);) {
a = p >= 0 ? s[p--] - '0' : 0;
b = isdigit(s[q]) ? s[q--] - '0' : 0;
t = a + b + up;
sum[j++] = t % 10;
up = t / 10;
}
if (up) sum[j] = up;
for (k = 0; isdigit(s[k]);) printf("%d", s[k++]-'0');
for (printf(" + "), k++; k < strlen(s) - 1;) printf("%d", s[k++]-'0');
for (printf(" = "), k = N-1; sum[k] == 0; k--) ;
for (; k >= 0; k--) printf("%d", sum[k]);
printf("\n");
if (i != n-1) printf("\n");
}
return 0;
}
供参考……减法应该类似……不难写出。