请注意题目中说明:
each line consists of two positive integers, A and B.
Notice that the integers are very large,(注意!!!very large!!!)
that means you should not process them by using 32-bit integer. (连int都保存不下!!!)
You may assume the length of each integer will not exceed 1000.
(每个整数可达上千位数)。
这题考察的是大数的处理。
用字符串模拟。
普通的A+B是无法通过这题的。
而且你的程序连样例都过不了(格式也不对,多空格少空格的),
请认真测试。
附上AC代码:#include
#include
#include
#include
#include
using namespace std;
int main()
{
int ca;
scanf("%d", &ca);
for (int i = 1; i <= ca; i++)
{
char a[2002], b[2002], c[1050] = { 0 };
memset(a, '0', sizeof(a));
memset(b, '0', sizeof(b));
scanf("%s%s", &a[1000], &b[1000]);
size_t la = strlen(&a[1000]), lb = strlen(&b[1000]);
char *pa = &a[1000] + la - 1, *pb = &b[1000] + lb - 1, *pc = &c[1020];
size_t maxl = la > lb ? la : lb;
*(pa - maxl) = 0;
*(pb - maxl) = 0;
int carry = 0;
while (*pa&&*pb)
{
int v = (int)*pa + (int)*pb - (int)'0' + carry;
carry = 0;
if (v > '9')
{
v -= 10;
carry = 1;
}
*pc-- = v;
pa--, pb--;
}
if (carry)
*pc = '1';
else
pc++;
printf("Case %d:\n%s + %s = %s\n", i, &a[1000], &b[1000], pc);
if (i != ca)
printf("\n");
}
return 0;
}