tags:

views:

45

answers:

2

Guys, got the answer in C ; THank you for all the help .

Hey ,

I did go through the other threads on this(ADDREV Problem )(https://www.spoj.pl/problems/ADDREV/ ) but sadly, i am not able to get an answer by any of the three programs that i have written. ( in C , python and java ) . I am attaching the code snippets of all three. Thanks a lot.

Python :

def find_rev(a):
    d=0

    while(a>=1):
        d=d*10+a%10
        a=a/10
    return d

n=input('enter a number')
for i in range(int(n)):
    num1=input('enter the first number')
    num2=input('enter the second number')
    num=0
    num1=find_rev(int(num1))
    num2=find_rev(int(num2))

    num=num1+num2
    num=find_rev(num)

    print num

With Python, i get runtime error.

With C, i get wrong answer.

#include<stdio.h>
long rev(long);
int main()
{
long int n;
long int n1;
long int n2;
long int i=0;
scanf("%ld",&n);
//printf("%d",n);
for(i=0;i<n;i++)
{

    //printf("\n%d",i);
//printf("\nenter the two numbers");
scanf("%ld%ld",&n1,&n2);

n=rev(rev(n1)+rev(n2));
printf("%ld\n",n);

}
return 0;
}

long rev(long a)
{
long d=0;
while(a>=1)
{
d=d*10+a%10;
a=a/10;
}
return d;
}

With Java, i get a compilation error .

import java.util.*;
//import java.io.*;
public class spoj_prob {

public static void main(String args[])
{
    long n=0;
    System.out.println("enter a number \n");
    Scanner in=new Scanner(System.in);
    n=in.nextLong();
    long n1=0;
    long n2=0;
    long sum=0;
    for(int i=0;i<n;i++)
    {
        System.out.println("enter two numbers \n ");
         n1=in.nextLong();
         n2=in.nextLong();
        n1=rev(n1);
        n2=rev(n2);
        System.out.println(n1);
        System.out.println(n2);
         sum=rev(n1+n2);
        System.out.println(sum);

    }
}
static long rev(long a)
{
    long d=0;
    while(a>=1)
    {
        d=d*10+a%10;
        a=a/10;
    }
    return d;

    }
}

Of course, those errors are reported by the SPOJ Judge. The programs work fine on my system . Test cases i use are :

2

999999999 11

999 11

Answer 101 101

Also 3

34 54

123 091

00034 00054

+1  A: 

Before you start using any service, it's generally a good thing to read its faq. It explains how exactly program should receive data.

In particular, please notice that printing enter a number and other junk to console will always lead to wrong answer. Because correct program would output something like

34
1998
1

and yours

enter a number
enter two numbers
34
enter two numbers
1998
enter two numbers
1

Can't tell while Java fails to compile, though. You probably should find some information on how to submit in Java with reference solution.

Also, problem gives no limit for input numbers, so they can possibly be too big for standard integer types in Java and C++.

Nikita Rybak
Thanks for the suggestions. After making changes in python by taking input just by using input() , it is giving NZEC ( non-zero exit code).
crazyaboutliv
A: 
  • With Python I think you're getting Runtime Error because you're calling a restricted function input, nothing else comes to mind.
  • In C you're getting WA because the input integers can be very large, and you're overflowing.
  • For JAVA there are 2 potential problems that you may have. One is that you're using Scanner class which may not be supported by SPOJ (due to security or other considerations). Second, is that your class name needs to be Main I think. Please search SPOJ forum for more details on this.
Leonid
In C, i have used long at all places. THe input numbers, even the loop variable and the function which reverses takes and returns a long int. That is giving compilation error itself.
crazyaboutliv