杭电acm1002,提交总是Wrong Answer

2024-11-20 03:20:05
推荐回答(1个)
回答1:

#include
#include

using namespace std;

int main ( )
{
int T,i,j,t,m,k,carry,temp;
char a[1001] , b[1001] ;
char c[1001] ,d[1001];
while (cin >> T )
{
for( t = 1 ; t <= T ; t++)
{
cin >> a >> b ;
int len1 = strlen(a);
int len2 = strlen(b);
k = 1000; // k初始化为1000
c[k]='0';
carry=0;
for(i = len1-1,j=len2-1; i>=0||j>=0; i--,j--,k--)
{
temp=carry;

if(i>=0)
temp+=a[i]-'0';
if(j>=0)
temp+=b[j]-'0';
if(temp>=10)
{
c[k]=temp-10+'0';
carry=1;
}
else
{
c[k]=temp+'0';
carry=0;
}

}
if(carry==1)
c[k]=carry+'0';
else // 如果最后没有进位,需要把k自增
k++;

for(m=0; k<=1000; m++,k++) // 复制到1000
d[m]=c[k];
d[m]='\0';

cout << "Case " << t <<":"<< endl; // C大写

cout << a << " + " << b << " = " << d << endl;
if(t!=T)
cout << endl;

}
}
system ("pause");
return 0;
}