Problem:
Integral Rectangles

Let us consider rectangles whose height, h, and
width, w, are both integers. We call such rectangles
integral rectangles. In this problem, we consider only wide
integral rectangles, i.e., those with w > h.

We define the following ordering of wide integral rectangles. Given
two wide integral rectangles,

The one shorter in its diagonal line is smaller, and

If the two have diagonal lines with the same length, the one shorter
in its height is smaller.

Given a wide integral rectangle, find the smallest wide integral
rectangle bigger than the given one.

Input

The entire input consists of multiple datasets. The number of
datasets is no more than 100. Each dataset describes a wide integral
rectangle by specifying its height and width, namely h
and w, separated by a space in a line, as follows.

h w

For each dataset, h and w (>h) are both
integers greater than 0 and no more than 100.

The end of the input is indicated by a line of two zeros separated by a space.

Output

For each dataset, print in a line two numbers describing the
height and width, namely h and w (> h), of the
smallest wide integral rectangle bigger than the one described in the
dataset. Put a space between the numbers. No other
characters are allowed in the output.

For any wide integral rectangle given in the input, the width and
height of the smallest wide integral rectangle bigger than the given
one are both known to be not greater than 150.

In addition, although
the ordering of wide integral rectangles uses the comparison of
lengths of diagonal lines, this comparison can be replaced with that
of squares (self-products) of lengths of diagonal lines, which can
avoid unnecessary troubles possibly caused by the use of floating-point
numbers.

Sample Input

1 2
1 3
2 3
1 4
2 4
5 6
1 8
4 7
98 100
99 100
0 0

Output for the Sample Input

1 3
2 3
1 4
2 4
3 4
1 8
4 7
2 8
3 140
89 109

Solution in JAVA (judge verdict: Accepted)

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int[] h = new int[100000];
		int[] w = new int[100000];
		int[] t = new int[100000];
		int n = 0;
		int a =1;
		int k=0;
		suuzi[] p =new suuzi[150*150];
		int q =0;
		int g=0;
		suuzi r ;
		suuzi b = new suuzi();
		for(int i=0;i<100000;i++){
			h[i]=sc.nextInt();
			w[i]=sc.nextInt();
			t[i]=h[i]*h[i]+w[i]*w[i];
			if(h[i]==0&&w[i]==0){
				n=i;
				break;
			}

		}
		for(int l=1;l<=150;l++){
			for(k=l+1;k<=150;k++){
				suuzi m = new suuzi();
				m.x=l;
				m.y=k;
				m.z=l*l+k*k;
				p[q]=m;
				q=q+1;
			}
		}
		q=q-1;
	   while(a==1){
		   a=0;
		   for(int i=0;i<q;i++){
			   if(p[i].z>p[i+1].z){
				   r=p[i];
				   p[i]=p[i+1];
				   p[i+1]=r;
				   a=1;
			   }
			   if(p[q].z==p[i+1].z&&p[i].x>p[i+1].x){
				   r=p[i];
				   p[i]=p[i+1];
				   p[i+1]=r;
				   a=1;
			   }
				   
		   }
	   }

		while(g<n-1){
			b.x =h[g];
			b.y =w[g];
			g=g+1;
				for(int l=0;l<q;l++){
					if(b.x==p[l].x&&b.y==p[l].y){
						System.out.println(p[l+1].x+" "+p[l+1].y);
						
		}
		
	}
				while(g==n-1){
					b.x =h[g];
					b.y =w[g];
					g=g+1;
						for(int l=0;l<q;l++){
							if(b.x==p[l].x&&b.y==p[l].y){
								System.out.println(p[l+1].x+" "+p[l+1].y);
								
				}
				
			}
				}

}
	}
}
class suuzi{
	int x;int y;int z;
}