oj上简单的大数相加~为什么我的不对

来源:百度知道 编辑:UC知道 时间:2024/05/28 05:22:44
http://acm.hdu.edu.cn/showproblem.php?pid=1002
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Samp

这是我初学高精度时候写的,AC了
#include<stdio.h>
#include<string.h>
int main()
{
int a[1001]={0},b[1001]={0},c[1001]={0},n;
int i,ka,kb,k,j=0;
char a1[1002],b1[1002];
scanf("%d",&n);
while(n--)
{
j++;
scanf("%s %s",a1,b1);
memset(a,0,1001*sizeof(int));
memset(b,0,1001*sizeof(int));
memset(c,0,1001*sizeof(int));
ka=strlen(a1);
kb=strlen(b1);
if(ka>=kb)
k=ka;
else
k=kb;
for(i=0;i<ka;i++)
a[i]=a1[ka-i-1]-'0';
for(i=0;i<kb;i++)
b[i]=b1[kb-i-1]-'0';
for(i=0;i<k;i++)
{
c[i]=a[i]+b[i]+c[i];
c[i+1]=c[i]/10;
c[i]=c[i]%10;
}
if(c[k])
k++;
printf("Case %d:\n",j);
for(i=0;i<ka;i++)
printf("%c",a1[i]);
printf(" + ");
for(i=0;i<kb;i++)
printf("%c",b1[i]);
printf(&quo