## 713 - Adding Reversed Numbers

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Morning
Experienced poster
Posts: 134
Joined: Fri Aug 01, 2003 2:18 pm
Location: Shanghai China
Thanks,everyone,i got AC(pe).Something wrong with my carry.
"Learning without thought is useless；thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York
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.

Frostina
New poster
Posts: 23
Joined: Mon Dec 15, 2003 5:21 am
Input:

Code: Select all

``````13245 4678746
100032 00018678
123 787951
``````
Output:

Code: Select all

``````5992356
10011978
801061
``````
Thanks for your help !

20717TZ
New poster
Posts: 33
Joined: Tue Apr 27, 2004 7:41 pm
Location: Santa Clara / Mountain View, CA, USA
Contact:
BigNumber Considered?
Is Array Range Enough?
'0's Deleted?
I Believe I Can - leestime.com

murtaza
New poster
Posts: 7
Joined: Tue Jul 20, 2004 7:42 pm
Location: India

### 713

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);
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;
}

Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology
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).

Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology
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).

Piotrek Mazur
New poster
Posts: 17
Joined: Thu Jul 15, 2004 10:55 am
Location: Poland, Rzeszow University of Technology
Forgot about my post above - I had one little error. Now is AC

Wei
New poster
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm
Contact:
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??

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan
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
``````
Last edited by Eduard on Mon Oct 04, 2004 8:19 pm, edited 2 times in total.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

Wei
New poster
Posts: 23
Joined: Sat Jul 24, 2004 5:37 pm
Contact:
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???

Eduard
Experienced poster
Posts: 183
Joined: Fri Sep 26, 2003 2:54 pm
Location: Armenia,Yerevan
It is interesting for me too.May be My AC solution is not complately right.Or may be 10+10 -----> 01+01=02 ----->20.
someone who like to solve informatic problems.
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650

WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am
My accepted solution returns 2 for both 10, 10 and 01, 01!

New poster
Posts: 18
Joined: Fri Jan 07, 2005 9:35 pm

### 713 why WA plz help

why am i getteing WA 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;
}