A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 294530 Accepted Submission(s): 56704
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.
说说我遇到的问题吧。
1:首先是读题的问题。不知道exceed 1000是什么意思,就定义了一个long long,想着这个数很大了呗,这个题和1000有什么区别啊,结果交上去不出意料的的了一个WA
.
2:知道了意思后,就想着怎么解决这个问题呢。数组是第一个想到的,可是数组的输入是个问题,因为不知道循环的次数,不知道怎么输。可是数组又必须用,memset函数必须上啊。最后就想了一个办法,先用gets用字符串录进去,字符串有strlen函数啊,这样次数问题就解决了。再把数组的数一个一个录进去就好了。
3:数组的存储问题。正着还是反着呢?代码部分会看到答案,大家肯定知道怎么选。
#include<stdio.h>
#include<string.h>
int main()
{
char d[1000],e[1000],al;
int a[1000],b[1000],c[1000],i=0,j,k=1,max1,max2,max;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(d);
max1=strlen(d)-1;
for(i=0;i<=max1/2;)
{
al=d[max1];
d[max1]=d[i];
d[i]=al;
i++;
max1--;;
}
gets(e);
max2=strlen(e)-1;
for(i=0;i<=max2/2;)
{
al=e[max2];
e[max2]=e[i];
e[i]=al;
i++;
max2--;;
}
for(i=0;i<strlen(d);i++)
{
a[i]=d[i]-48;
}
for(i=0;i<strlen(e);i++)
{
b[i]=e[i]-48;
}
max=(strlen(d)>strlen(e)?strlen(d):strlen(e));
c[0]=(a[0]+b[0])%10;
while(k<=max)
{
c[k]=(a[k]+b[k])%10+(a[k-1]+b[k-1])/10;
k++;
}
if(c[max]!=0)
printf("%d",c[max]);
k=max-1;
for(k;k>=0;k--)
{
printf("%d",c[k]);
}
}
#include<string.h>
int main()
{
char d[1000],e[1000],al;
int a[1000],b[1000],c[1000],i=0,j,k=1,max1,max2,max;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(d);
max1=strlen(d)-1;
for(i=0;i<=max1/2;)
{
al=d[max1];
d[max1]=d[i];
d[i]=al;
i++;
max1--;;
}
gets(e);
max2=strlen(e)-1;
for(i=0;i<=max2/2;)
{
al=e[max2];
e[max2]=e[i];
e[i]=al;
i++;
max2--;;
}
for(i=0;i<strlen(d);i++)
{
a[i]=d[i]-48;
}
for(i=0;i<strlen(e);i++)
{
b[i]=e[i]-48;
}
max=(strlen(d)>strlen(e)?strlen(d):strlen(e));
c[0]=(a[0]+b[0])%10;
while(k<=max)
{
c[k]=(a[k]+b[k])%10+(a[k-1]+b[k-1])/10;
k++;
}
if(c[max]!=0)
printf("%d",c[max]);
k=max-1;
for(k;k>=0;k--)
{
printf("%d",c[k]);
}
}