UVA – 10018 – Reverse And Add

  • ID : UVA – 10018 – Reverse And Add
  • Submissions : Java – Accepted
  • Difficulty : Easy
  • Type : Simple Math, Plaindromes
  • Time for Submission : 20 minutes
  • Solution Description :
    • two methods one reverse the digits and the other recursively finds the solution.
  • Problems :
    • No Problems, it went smoothly 🙂
  • Code :

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int T = in.nextInt();
		while(T--!=0){
			long[] ans =count(0,in.nextInt());
			System.out.println(ans[0] +" "+ ans[1]);
		}
	}
	public static long[] count(long i, long n) {

		if (reverse(n) == n)
			return new long[] { i, n };

		return count(i + 1, reverse(n) + n);
	}

	public static long reverse(long num) {
		long dig = 0, rev = 0;
		while (num > 0) {
			dig = num % 10;
			rev = rev * 10 + dig;
			num = num / 10;
		}
		return rev;
	}
}
Advertisements

One thought on “UVA – 10018 – Reverse And Add

  1. Carey says:

    It’s going to be end of mine day, except before end I am reading this fantastic post to improve my knowledge.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: