Page 4 of 10

Posted: Sat Jan 31, 2004 9:15 am
by Morning
Thanks,everyone,i got AC(pe).Something wrong with my carry.

Posted: Sun Feb 01, 2004 4:27 pm
by sohel
Now I know what my problem is.

I have to consider the numbers as string.

But what is wrong with the previous problem. Instead of changing an old problem they should set new question based on the change.

THE problem set should be a mixture of easy and hard problems and if they transform all to harder ones then the novices will lose interst. Which can not be good as a whole.

The previous problem was a perfect one for a beginner.
:roll:

Posted: Tue Feb 03, 2004 6:53 am
by Frostina
Input:

Code: Select all

13245 4678746
100032 00018678
123 787951
Output:

Code: Select all

5992356
10011978
801061

Posted: Sat May 08, 2004 3:26 pm
by 20717TZ
BigNumber Considered?
Is Array Range Enough?
'0's Deleted?

713

Posted: Tue Jul 20, 2004 7:51 pm
by murtaza
I saw all the help on prob.713 and tried all the suggested inputs .... but still i keep getting WA.
Can someone plz lokk at my code below and suggest.......

C code:
---------

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

/*find the revesed sum of 2 numbers .... numbers r upto 200 digits long!! */

char * reverse(char *,char *);
char * add(char *,char *,char *);

char num1[1001],num2[1001];
main()
{
char *rev1, *rev2, *result,* reversed;
int numCases,k = 0,flag = 0,j = 0,i;
rev1 = NULL;
rev2 = NULL;
result = NULL;
reversed = NULL;

scanf("%d",&numCases);
for(i = 0;i<numCases;i++)
{
scanf("%s %s",num1,num2);
rev1 = reverse(num1,rev1);
rev2 = reverse(num2,rev2);
result = add(rev1,rev2,result);
reversed = reverse(result,reversed);
if(rev1[0] == '\0' && rev2[0] == '\0')
printf("0\n");
else
printf("%s\n",reversed);
}
}


char * reverse(char * num,char * rev)
{
int len,i,flag = 0,j; /*if flag is '0' then no non-zero number has
been encountered so far*/
char *temp;
len = strlen(num);
temp = (char *)malloc(sizeof(char) * len);

j = 0;
for(i = len-1;i>-1;i--)
{
if(flag==0)
{
if(num == '0')continue;
}
flag = 1;
temp[j] = num;
j++;
}
temp[j] = '\0';
rev = temp;
return temp;
}

char * add(char * op1,char * op2, char * res)
{
int len1,len2,i,j;
int carry = 0; /*this flag is set when there is a carry generated from the lower digits*/
char * temp,tp;
len1 = strlen(op1);
len2 = strlen(op2);

if(len1>len2)
{
temp = (char *)malloc(sizeof(char) * len1);
i = len1 + 1;
}
else
{
temp = (char *)malloc(sizeof(char) * len2);
i = len2 + 1;
}

temp = '\0';
i--;
len1--;
len2--;
while(len1>= 0 && len2 >= 0)
{
if((op1[len1] - '0' + op2[len2] - '0' + carry) >9)
{/*there is a carry*/
temp = op1[len1] - '0' + op2[len2] + carry - 10;
carry = 1;
}
else
{
temp = op1[len1] + op2[len2] - '0' + carry;
carry = 0;
}
len1--;
len2--;
i--;
}
if(len1<0 && len2<0)
{
if(carry)
{
temp = '1';
i--;
}
}
else if(len1<0)
{
while(len2>=0)
{
if((op2[len2] - '0' + carry) > 9)
{
temp = op2[len2] + carry - 10 ;
carry = 1;
}
else
{
temp = op2[len2] + carry;
carry = 0;
}
len2 -- ;
i--;
}
if(carry)
{
temp = '1';
i--;
}
}
else
{
while(len1>=0)
{
if((op1[len1] - '0' + carry) > 9)
{
temp = op1[len1] + carry - 10;
carry = 1;
}
else
{
temp[i] = op1[len1] + carry;
carry = 0;
}
len1--;
i--;
}
if(carry){temp[i] = '1';i--;}
}
if(i==0)
{
i = 1;
while(temp[i]!='\0')
{ temp[i-1] = temp[i];
/*printf("110 : %c\n",temp[i]);*/
i++;
}
temp[i-1]='\0';
}
res = temp;
return temp;
}



Thanks in advance

Posted: Sat Jul 31, 2004 7:27 pm
by Piotrek Mazur
Can anyone give more sample input & output for this problem? I don't understand cleary when I have to replace zeros (leading zeros after adding, as the problem description says, and I tried to remove and not remove trailing zeros before adding).

Posted: Sun Aug 01, 2004 11:07 am
by Piotrek Mazur
I have tested judge's input of leading and trailing zeroes: and I think there are no test cases with it (but input like '0 0' can be).

Posted: Fri Aug 13, 2004 10:39 pm
by Piotrek Mazur
Forgot about my post above - I had one little error. Now is AC :)

Posted: Tue Sep 28, 2004 9:09 am
by Wei
I don't know why~~
I have the right answers for these test datas...
But I still get a WA~~~
Could somebody give me some special test datas??

Posted: Tue Sep 28, 2004 10:53 am
by Eduard
Hello
I think you know that you must use big integers.Here are some tests.
INPUT

Code: Select all

14
123123 12313213213
11111111 111111111111
565656565656 45646546
1 1
232 2323
123 13
1233333333333 123333333333
123123 1323333333
12333333333333 123333333333333
11 11
12 12
15 15
10 10
01 01

OUTPUT

Code: Select all

24625513213
222222221111
912122036656
2
4643
253
2466666666663
2554563333
246666666666663
22
24
201
20
2

Posted: Mon Oct 04, 2004 3:53 pm
by Wei
Well~~Thx for giving me so many test datas.
But I still got WA~~
And I didn't know why when input were 10 10, the output should be 20???

Posted: Mon Oct 04, 2004 8:17 pm
by Eduard
It is interesting for me too.May be My AC solution is not complately right.Or may be 10+10 -----> 01+01=02 ----->20. :(

Posted: Tue Oct 05, 2004 3:41 pm
by WR
My accepted solution returns 2 for both 10, 10 and 01, 01!

713 why WA plz help

Posted: Sun Apr 03, 2005 5:02 pm
by Fuad Hassan_IIUC(DC)
why am i getteing WA :o plz help me

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void strrev1(char *str1,char *str2)
{
long long len,i,j,k;
len=strlen(str1);
for(i=0;i<=len;i++)
str2=str1[len-i-1];
}

int main()
{
char buffer[100000],bufferrev[100000],fuffer[100000],fufferrev[100000];
long long i,j,k,l;
while(cin>>k)
{

for(l=0;l<k;l++)
{
cin>>i>>j;
sprintf(buffer,"%lld",i);
sprintf(fuffer,"%lld",j);
strrev1(buffer,bufferrev);
strrev1(fuffer,fufferrev);
sscanf(bufferrev,"%lld",&i);
sscanf(fufferrev,"%lld",&j);
int sum=i+j;
char suffer[100000],sufferrev[100000];
sprintf(suffer,"%lld",sum);
strrev1(suffer,sufferrev);
sscanf(sufferrev,"%lld",&sum);
// cout<<i<<endl<<j<<endl;
cout<<sum<<endl;
}
}
return 0;
}

Posted: Sun Apr 03, 2005 7:06 pm
by CodeMaker
The problem says, the input numbers will be almost 200 digits long. Do you think long long is going to hold 200 digit long number. definitely no. Use Big number to solve it. :-?