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

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

Re: 713 - Adding Reversed Numbers

Post by mentalist_mbstu » Sun Nov 24, 2013 5:38 am

i don't understand could u tell in details

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

Re: 713 - Adding Reversed Numbers

Post by mentalist_mbstu » Mon Nov 25, 2013 9:11 am

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

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

Re: 713 - Adding Reversed Numbers

Post by brianfry713 » Tue Nov 26, 2013 1:06 am

Input:

Code: Select all

2
999999 999999
1 999999
AC output:

Code: Select all

8999991
1
See http://ideone.com/gnCDWB
Check input and AC output for thousands of problems on uDebug!

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

Re: 713 - Adding Reversed Numbers

Post by mentalist_mbstu » Wed Nov 27, 2013 12:05 pm

brianfry713 wrote:Input:

Code: Select all

2
999999 999999
1 999999
AC output:

Code: Select all

8999991
1
See http://ideone.com/gnCDWB
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

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

Re: 713 - Adding Reversed Numbers

Post by brianfry713 » Wed Dec 04, 2013 2:09 am

You're reading past the end of a string.
Check input and AC output for thousands of problems on uDebug!

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

Re: 713 - Adding Reversed Numbers

Post by mentalist_mbstu » Fri Dec 06, 2013 11:11 am

i don't understand what u want to say
brianfry713 wrote:You're reading past the end of a string.

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

Re: 713 - Adding Reversed Numbers

Post by brianfry713 » Fri Dec 06, 2013 9:34 pm

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!

mentalist_mbstu
New poster
Posts: 12
Joined: Mon Nov 18, 2013 10:37 pm

Re: 713 - Adding Reversed Numbers

Post by mentalist_mbstu » Sat Dec 07, 2013 2:49 pm

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.

MNT.95
New poster
Posts: 7
Joined: Thu Jan 23, 2014 5:40 pm

Re: 713 - Adding Reversed Numbers

Post by MNT.95 » Sun Feb 09, 2014 12:32 pm

My code is AC in SPOJ and POJ, but in UVA it is WA??
Why is that heppens to the same code ??

MNT.95
New poster
Posts: 7
Joined: Thu Jan 23, 2014 5:40 pm

Re: 713 - Adding Reversed Numbers

Post by MNT.95 » Sun Feb 09, 2014 12:45 pm

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

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

Re: 713 - Adding Reversed Numbers

Post by brianfry713 » Mon Feb 10, 2014 11:17 pm

use scanf("%lld") for a long long
Check input and AC output for thousands of problems on uDebug!

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

Re: 713 - Adding Reversed Numbers

Post by uDebug » Tue Mar 04, 2014 9:40 am

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

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

Re: 713 - Adding Reversed Numbers

Post by uDebug » Tue Mar 04, 2014 9:43 am

Replying to follow the thread.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

try_tired
New poster
Posts: 6
Joined: Thu Jan 23, 2014 3:22 am

Re: 713 - Adding Reversed Numbers

Post by try_tired » Sun Mar 16, 2014 12:12 am

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

}

}

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

Re: 713 - Adding Reversed Numbers

Post by uDebug » Sun Mar 16, 2014 8:14 am

try_tired wrote:Why I got wrong ans: here.......... pls help. :(
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?

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.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 7 (700-799)”