First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

Reverse and Add.

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-10018
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=959

Code:

import java.util.*;
public class Uva_10018 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int Tomato=in.nextInt();
while(Tomato--!=0){
long[] jisan=count(0,in.nextInt());
System.out.println(jisan[0]+" "+jisan[1]);
}
}
public static long[] count(long a,long b){
if(reverse(b)==b){
return new long[] {a,b};
}
return count(a+1,reverse(b)+b);
}
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;
}
}

No comments:

Post a Comment