## 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

pnkj_agrl
New poster
Posts: 2
Joined: Thu May 08, 2014 9:36 pm

### Re: 713 - Adding Reversed Numbers

i got accepted in both spoj and codechef but giving wrong answer in uva why is it so??
why? why??

#include<cstdio>
int main()
{
long long int t,a,b,p,reverse,reverse1,reverse2;
scanf("%lld",&t);
while(t--)
{
reverse=0;
reverse1=0;
reverse2=0;
scanf("%lld%lld",&a,&b);
while(a!=0)
{
reverse=reverse*10;
reverse=reverse+(a%10);
a/=10;
}
while(b!=0)
{
reverse1=reverse1*10;
reverse1=reverse1+(b%10);
b/=10;
}
p=reverse+reverse1;
while(p!=0)
{
reverse2=reverse2*10;
reverse2=reverse2+(p%10);
p/=10;
}
printf("%lld",reverse2);
printf("\n");
}
return 0;
}

Zyaad Jaunnoo
Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

### Re: 713 - Adding Reversed Numbers

brianfry713 wrote:Try input:

Code: Select all

``````1
30 30``````
Output should be 60
My accepted program returns 6 instead of 60. Here are the steps below.

Code: Select all

``````Reverse ( Reverse(30) + Reverse(30) ) = Reverse (3 + 3)
= Reverse (6)
= 6
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 713 - Adding Reversed Numbers

v1n1t, your input is invalid. I also deleted my post with tailing zeros input. In the judge's input there are no test cases with trailing zeros. From the problem statement "Also note that the reversed number never has any trailing zeros."
Check input and AC output for thousands of problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: 713 - Adding Reversed Numbers

brianfry713 wrote:v1n1t, your input is invalid. I also deleted my post with tailing zeros input. In the judge's input there are no test cases with trailing zeros. From the problem statement "Also note that the reversed number never has any trailing zeros."
Thanks for bringing this to my attention, brianfry713. I've deleted my test cases.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

xrenon
New poster
Posts: 10
Joined: Tue Sep 23, 2014 4:11 am

### Re: 713 - Adding Reversed Numbers

Deleted
Last edited by xrenon on Thu Sep 25, 2014 9:20 am, edited 1 time in total.

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

### Re: 713 - Adding Reversed Numbers

I am not good in Java. I think you get RE because of Integer.parseInt. You use it for Biginteger. Change your code

Code: Select all

``````BigInteger a = new BigInteger(sb1.reverse().toString());

BigInteger b = new BigInteger(sb2.reverse().toString());

BigInteger num = a.add(b);

StringBuilder sb = new StringBuilder(num.toString()).reverse();

num = new BigInteger(sb.toString());

System.out.println(num);``````
Don't forget to remove your code after getting accepted.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

xrenon
New poster
Posts: 10
Joined: Tue Sep 23, 2014 4:11 am

### Re: 713 - Adding Reversed Numbers

Thanks a lot....deleted
Last edited by xrenon on Mon Nov 03, 2014 6:42 pm, edited 1 time in total.

lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

### Re: 713 - Adding Reversed Numbers

Please use code tags. It is difficult to understand your code

Don't use Integer.parseInt for big numbers. Use Integer.parseInt for integers.

For this version of your code change following part

Code: Select all

``````s1 = new BigInteger("" + Integer.parseInt(ck1.reverse().toString()));
s2 = new BigInteger("" + Integer.parseInt(ck2.reverse().toString()));
``````
It must be

Code: Select all

``````s1 = new BigInteger(ck1.reverse().toString());
s2 = new BigInteger(ck2.reverse().toString());
``````
Also remove br.close().
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman