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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		IntegralRect rects[] = new IntegralRect[40000];
		for(int i = 0; i < 200; i++){
			for(int j = 0; j < 200; j++){
				rects[i*200 + j] = new IntegralRect(i + 1, j + 1);
			}
		}
		Arrays.sort(rects);
		
		//debug
//		for(int i = 0; i < 100; i++){
//			System.out.println(rects[i].height + " "+ rects[i].width +" " + rects[i].diagonal());
//		}

		while(true){
			String[] tmpArray = br.readLine().split(" ");

			int h = Integer.parseInt(tmpArray[0]);
			int w = Integer.parseInt(tmpArray[1]);

			if(h == 0 && w == 0){
				break;
			}

			IntegralRect tmpRect = new IntegralRect(h, w);
			int diag = tmpRect.diagonal();
//			System.out.println(diag);
			for(int i = 0; i < rects.length ; i++){
//				System.out.println(rects[i].diagonal());
				if(rects[i].compareTo(tmpRect) > 0){
					System.out.println(rects[i].height+" "+rects[i].width);
					break;
				}
			}
		}
	}

}

class IntegralRect implements Comparable<IntegralRect> {
	int height;
	int width;
	public IntegralRect(int h, int w){
		this.height = h;
		this.width = w;
	}


	public int diagonal(){
		if(height >= width){
			return Integer.MAX_VALUE;
		}
		return this.height*this.height + this.width*this.width;
	}


	@Override
	public int compareTo(IntegralRect o) {
		// TODO ?????????????????????????????????????????????
		int d1 = this.diagonal();
		int d2 = o.diagonal();
//		System.out.println(d1+" "+d2);
		return d1 == d2 ? this.height - o.height : d1 - d2;
	}
}