SPOJ Adding Reversed Numbers

  1. import java.util.*;
  2. import java.math.*;
  3. class Main
  4. {
  5. public static void main (String[] args)
  6. {
  7. Scanner in = new Scanner (System.in);
  8. int t,tc,n=10;
  9. tc = in.nextInt();
  10. for (t=1;t<=tc;t++)
  11. {
  12. BigInteger a,b,x,y,s1=BigInteger.valueOf(0),s2=BigInteger.valueOf(0),p=BigInteger.valueOf(10),s,r=BigInteger.valueOf(0),m;
  13. x=in.nextBigInteger(); y = in.nextBigInteger();
  14. a=x;b=x;
  15. while (a!=BigInteger.valueOf(0))
  16. {
  17. a = a.mod(p);
  18. s1 = a.add(s1.multiply(p));
  19. b = b.divide(p);
  20. a=b;
  21. }
  22. //System.out.print(s1);
  23. a=y;b=y;
  24. while (a!=BigInteger.valueOf(0))
  25. {
  26. a = a.mod(p);
  27. s2 = a.add(s2.multiply(p));
  28. b = b.divide(p);
  29. a=b;
  30. }
  31. s = s1.add(s2);
  32. //System.out.print(s);
  33. a = s ; b = s;
  34. while (a!=BigInteger.valueOf(0))
  35. {
  36. a=a.mod(p);
  37. r = a.add(r.multiply(p));
  38. b = b.divide(p);
  39. if (r == BigInteger.valueOf(0))
  40. {
  41. r = BigInteger.valueOf(0);
  42. }
  43. a = b;
  44. }
  45. System.out.println(r);
  46. }
  47. }
  48. }

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number