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;
}
713 - Adding Reversed Numbers
Moderator: Board moderators
-
- Experienced poster
- Posts: 122
- Joined: Tue Apr 16, 2002 10:07 am
Re: 713 - Adding Reversed Numbers
My accepted program returns 6 instead of 60. Here are the steps below.brianfry713 wrote:Try input:Output should be 60Code: Select all
1 30 30
Code: Select all
Reverse ( Reverse(30) + Reverse(30) ) = Reverse (3 + 3)
= Reverse (6)
= 6
-
- 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!
Re: 713 - Adding Reversed Numbers
Thanks for bringing this to my attention, brianfry713. I've deleted my test cases.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."
Re: 713 - Adding Reversed Numbers
Deleted
Last edited by xrenon on Thu Sep 25, 2014 9:20 am, edited 1 time in total.
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
Don't forget to remove your code after getting accepted. ![8)](./images/smilies/icon_cool.gif)
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);
![8)](./images/smilies/icon_cool.gif)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
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.
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 partIt must be
Also remove br.close().
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()));
Code: Select all
s1 = new BigInteger(ck1.reverse().toString());
s2 = new BigInteger(ck2.reverse().toString());
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman