Sphere Online Judge

SPOJ Problem Set (classical)

42. Adding Reversed Numbers

Problem code: ADDREV

The Antique Comedians of Malidinesia prefer comedies to tragedies. Unfortunately, most of the ancient plays are tragedies. Therefore the dramatic advisor of ACM has decided to transfigure some tragedies into comedies. Obviously, this work is very hard because the basic sense of the play must be kept intact, although all the things change to their opposites. For example the numbers: if any number appears in the tragedy, it must be converted to its reversed form before being accepted into the comedy play.

Reversed number is a number written in arabic numerals but the order of digits is reversed. The first digit becomes last and vice versa. For example, if the main hero had 1245 strawberries in the tragedy, he has 5421 of them now. Note that all the leading zeros are omitted. That means if the number ends with a zero, the zero is lost by reversing (e.g. 1200 gives 21). Also note that the reversed number never has any trailing zeros.

ACM needs to calculate with reversed numbers. Your task is to add two reversed numbers and output their reversed sum. Of course, the result is not unique because any particular number is a reversed form of several numbers (e.g. 21 could be 12, 120 or 1200 before reversing). Thus we must assume that no zeros were lost by reversing (e.g. assume that the original number was 12).

Input

The input consists of N cases (equal to about 10000). The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly one line with two positive integers separated by space. These are the reversed numbers you are to add.

Output

For each case, print exactly one line containing only one integer - the reversed sum of two reversed numbers. Omit any leading zeros in the output.

Example

Sample input: 

3
24 1
4358 754
305 794

Sample output:

34
1998
1

Added by:Adrian Kosowski
Date:2004-06-06
Time limit:5s
Source limit:50000B
Languages:All except: TECS
Resource:ACM Central European Programming Contest, Prague 1998

hide comments
2010-08-31 07:59:07 sanele
can someone help you must check if enter case is less than 10000 or the number of case must be less than 1000
2010-08-29 08:58:18 Chandra sekar
Pls remove ur source code here and post it in the forum.
http://www.spoj.pl/forum/
2010-08-29 05:06:44 rajasekar
pls any one help me
2010-08-29 04:11:15 rajasekar
hi my code is this its give correct answer in my tarbo C complier but in Spoj it produce result as wrong answer pls help me
#include<stdio.h>

int reverse(int );
int main()
{
int n,i,no1[100],no2[100],y[100],x[100],z[100],x1,y1,z1;

scanf("%d",&n);
n=n%99;
for(i=0;i<n;i++)
{
scanf("%d%d",&x1,&y1);
no1[i]=x1;
no2[i]=y1;
y[i]=reverse(x1);
z[i]=reverse(y1);
z1=y[i]+z[i];
x[i]=reverse(z1);

}
for(i=0;i<n;i++)
{
printf("\n%d",x[i]);
}
return 0;
}
int reverse (int x)
{
int temp,y=0,n,i;
for(i=0;x!=0;i++)
{
temp=x%10;
x=x/10;
if((temp!=0)||(temp==0&&x!=0))
y=(y*10)+temp;
}
return y;
}
2010-08-25 19:54:29 rajasekar
hi what is SIGSEGV error
i got all the things correct except this error could any one help me
2010-07-07 07:11:58 Mederbek Akmatov
Who are you Junior vasquez?
2010-06-30 06:29:43 Prachetaa
result: success time: 0.01s memory: 6360 kB returned value: 0

input:
2
12 41
13 40
output:
53
53

But spoj says wrong answer.
2010-06-24 23:27:32 Junior Vasquez
easy..
2010-06-22 22:42:13 Jessica Thomas
@pranav you are not suppose to leave any space (before or after input)
2010-06-18 12:29:57 pranav
i m getting the correct ans for the test cases mentioned,apart from the ones i have tested myself.i m getting wrong answer on submission, with time = 0.01s.somebody pls tell!!is there some criteria of leaving a space after input has been given and then displaying the output..??

Last edit: 2010-06-18 12:31:01
SPOJ System © 2010 Sphere Research Labs. All Rights Reserved.