713 - Adding Reversed Numbers
Moderator: Board moderators
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.

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.

Input:
Output:
Code: Select all
13245 4678746
100032 00018678
123 787951
Code: Select all
5992356
10011978
801061
Thanks for your help ! 

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);
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
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
-
- New poster
- Posts: 17
- Joined: Thu Jul 15, 2004 10:55 am
- Location: Poland, Rzeszow University of Technology
-
- New poster
- Posts: 17
- Joined: Thu Jul 15, 2004 10:55 am
- Location: Poland, Rzeszow University of Technology
-
- New poster
- Posts: 17
- Joined: Thu Jul 15, 2004 10:55 am
- Location: Poland, Rzeszow University of Technology
Hello
I think you know that you must use big integers.Here are some tests.
INPUT
OUTPUT
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
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
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
http://acm.uva.es/cgi-bin/OnlineJudge?AuthorInfo:29650
-
- New poster
- Posts: 18
- Joined: Fri Jan 07, 2005 9:35 pm
- Location: Bangladesh
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;
}

#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;
}
fuad