本题考察的就是高精度,这一点也能算作是编程入门基础了。加上之前ACM练习的第一题就是这个,所以印象深刻,主要考察模拟。
/*这个题目考察的就是高精度*/
#include <stdio.h>
#include <string.h>
int main() {
char a[1005], b[1005], c[2000];
while (scanf("%s %s", a, b) != EOF) {
int n1 = strlen(a) - 1, n2 = strlen(b) - 1;
int ws = 0, jw = 0;
while (n1 >= 0 && n2 >= 0) {
int t = a[n1] - '0' + b[n2] - '0' + jw;
jw = t / 10;
c[ws] = t % 10 + '0';
n1--, n2--, ws++;
}
if (n1 < 0) {
while (n2 >= 0) {
int t = b[n2] - '0' + jw;
jw = t / 10;
c[ws] = t % 10 + '0';
n2--, ws++;
}
} else {
while (n1 >= 0) {
int t = a[n1] - '0' + jw;
jw = t / 10;
c[ws] = t % 10 + '0';
n1--, ws++;
}
}
for (int i = ws - 1; i >= 0; i--) {
printf("%c", c[i]);
}
printf("\n");
}
return 0;
}

京公网安备 11010502036488号