713 - Adding Reversed Numbers
Moderator: Board moderators
-
- New poster
- Posts: 12
- Joined: Mon Nov 18, 2013 10:37 pm
Re: 713 - Adding Reversed Numbers
i don't understand could u tell in details
-
- New poster
- Posts: 12
- Joined: Mon Nov 18, 2013 10:37 pm
Re: 713 - Adding Reversed Numbers
brianfry713 wrote:On my machine your code doesn't match the sample input. It is printing some extra characters on the third line.
i don't understand could u tell in details
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 713 - Adding Reversed Numbers
Input:AC output:See http://ideone.com/gnCDWB
Code: Select all
2
999999 999999
1 999999
Code: Select all
8999991
1
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 12
- Joined: Mon Nov 18, 2013 10:37 pm
Re: 713 - Adding Reversed Numbers
brianfry713 wrote:Input:AC output:Code: Select all
2 999999 999999 1 999999
See http://ideone.com/gnCDWBCode: Select all
8999991 1
bt when i input
2
1 999999
999999 999999
output
1
8999991
bt when i input
2
999999 999999
1 999999
output
8999991
99991
i could not catch it
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 713 - Adding Reversed Numbers
You're reading past the end of a string.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 12
- Joined: Mon Nov 18, 2013 10:37 pm
Re: 713 - Adding Reversed Numbers
i don't understand what u want to say
brianfry713 wrote:You're reading past the end of a string.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 713 - Adding Reversed Numbers
A string is terminated with a null character. Your code continues to read beyond that into uninitialized data.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 12
- Joined: Mon Nov 18, 2013 10:37 pm
Re: 713 - Adding Reversed Numbers
i am new in string..so would u like to specify which line i make mistake?
brianfry713 wrote:A string is terminated with a null character. Your code continues to read beyond that into uninitialized data.
Re: 713 - Adding Reversed Numbers
My code is AC in SPOJ and POJ, but in UVA it is WA??
Why is that heppens to the same code ??
Why is that heppens to the same code ??
Re: 713 - Adding Reversed Numbers
My code
Code: Select all
#include<stdio.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long x,rev_x=0,y,rev_y=0,sum,rev_sum=0;
scanf("%d %d",&x,&y);
while(x>0)
{
rev_x *= 10;
rev_x += x%10;
x=x/10;
}
while(y>0)
{
rev_y *= 10;
rev_y += y%10;
y=y/10;
}
sum=rev_x+rev_y;
while(sum>0)
{
rev_sum *= 10;
rev_sum += sum%10;
sum=sum/10;
}
printf("%d\n",rev_sum);
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 713 - Adding Reversed Numbers
use scanf("%lld") for a long long
Check input and AC output for thousands of problems on uDebug!
Re: 713 - Adding Reversed Numbers
brianfry713,
Thanks very much for the host of test cases.
Update: Deleted own test cases per post by brianfry713.
Thanks very much for the host of test cases.
Update: Deleted own test cases per post by brianfry713.
Last edited by uDebug on Wed Aug 27, 2014 7:17 am, edited 1 time in total.
Re: 713 - Adding Reversed Numbers
Why I got wrong ans: here.......... pls help.
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args)throws IOException {
try{
String s,ss,sum1;
StringBuffer s1,s11,sum2;
BigInteger sum;
long l1,l2,lf;
int n,i;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i = 1;i <= n;i++)
{
s = sc.next();
ss = sc.next();
s1 = new StringBuffer(s);
s1 = s1.reverse();
s = s1.toString();
l1 = Long.valueOf(s);
s11 = new StringBuffer(ss);
s11 = s11.reverse();
ss = s11.toString();
l2 = Long.valueOf(ss);
sum = BigInteger.valueOf(l1+l2);
sum1 = sum.toString();
sum2 = new StringBuffer(sum1);
sum2 = sum2.reverse();
sum1 = sum2.toString();
lf = Long.valueOf(sum1);
System.out.println(lf);
}
}catch(Exception e){
System.out.print("Error");
}
}
}

import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args)throws IOException {
try{
String s,ss,sum1;
StringBuffer s1,s11,sum2;
BigInteger sum;
long l1,l2,lf;
int n,i;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(i = 1;i <= n;i++)
{
s = sc.next();
ss = sc.next();
s1 = new StringBuffer(s);
s1 = s1.reverse();
s = s1.toString();
l1 = Long.valueOf(s);
s11 = new StringBuffer(ss);
s11 = s11.reverse();
ss = s11.toString();
l2 = Long.valueOf(ss);
sum = BigInteger.valueOf(l1+l2);
sum1 = sum.toString();
sum2 = new StringBuffer(sum1);
sum2 = sum2.reverse();
sum1 = sum2.toString();
lf = Long.valueOf(sum1);
System.out.println(lf);
}
}catch(Exception e){
System.out.print("Error");
}
}
}
Re: 713 - Adding Reversed Numbers
First, consider using the code tags to share your code to make it more readable. Think about it: Would you spend even a moment looking at what you posted above as an objective reviewer?try_tired wrote:Why I got wrong ans: here.......... pls help.
Next, try searching the forums more extensively before you post. Here's a much more detailed thread with test cases and discussion.
http://acm.uva.es/board/viewtopic.php?f ... &hilit=713
Test your code with test cases given there.