开发者

SPOJ ADDREV Problem

I did go through the other threads on this SPOJ problem, ADDREV (Adding Reversed Numbers), but sadly, I was 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.

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 a runtime error.

With C, I get a 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

Update: Guys, I got the answer in C. Thank you for all the help.


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

In particular, please notice that printing enter a number and other junk to the console will always lead to a wrong answer. Because a 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

I can't tell why Java fails to compile, though. You probably should find some information on how to submit in Java with the reference solution.

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


  • 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.


Try this solution in Python 3:

import sys
t = int(input())
i=0
while i<t:
  a,b = map(int,sys.stdin.readline().split())    #to take both inputs in single line
  c=str(a)                                       #converting the number into string
  d=str(b)
  e=int(c[::-1])                    #converting reverse of the string c to int
  f=int(d[::-1])                    #converting reverse of the string d to int
  s=e+f                             #adding the two reverse numbers
  s1=str(s)
  s2=int(s1[::-1])                  #reverse s and display it
  print(s2)                                                     
  i+=1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜