给出两个正整数A和B,计算A+B的值。保证A和B的位数不超过500位。
读入两个用空格隔开的正整数
输出A+B的值
3 12
15
两个正整数的位数不超过500位
思路简单
#include<stdio.h> #include<string.h> int main(){ char a[505], b[505],c[505],d[505]; int sum[505]; memset(sum, 0, sizeof(sum)); /*gets_s(c); gets_s(d);*/ scanf("%s%s", c, d); int i, j, tmp, t = 0; for (i = strlen(c) - 1, j = 0; i >= 0; i--, j++) a[j] = c[i]; a[j] = '\0'; for (i = strlen(d) - 1, j = 0; i >= 0; i--, j++) b[j] = d[i]; b[j] = '\0'; for (i = 0; a[i] != '\0'&&b[i] != '\0'; i++){ tmp = a[i] - '0' + b[i] - '0' + t; t = tmp / 10; sum[i] = tmp % 10; } while (a[i] != 0){ tmp = a[i] - '0' + t; t = tmp / 10; sum[i++] = tmp % 10; } while (b[i] != 0){ tmp = b[i] - '0' + t; t = tmp / 10; sum[i++] = tmp % 10; } for (i = 501; sum[i] == 0; i--); while (i >= 0) printf("%d", sum[i--]); puts(""); return 0; }
原文地址:http://blog.csdn.net/u013174702/article/details/44174321