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 C++ (judge verdict: Accepted)


#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

struct hako {
	int tate;
	int yoko;
};
bool biggerthan(hako a, hako b) {
	int a2 = (a.tate)*(a.tate) + (a.yoko)*(a.yoko);
	int b2 = (b.tate)*(b.tate) + (b.yoko)*(b.yoko);
	if (a2 != b2) return a2 > b2;
	return a.tate > b.tate;
}
bool smallerthan(hako b, hako a) {
	int a2 = (a.tate) * (a.tate) + (a.yoko) * (a.yoko);
	int b2 = (b.tate) * (b.tate) + (b.yoko) * (b.yoko);
	if (a2 != b2) return a2 > b2;
	return a.tate > b.tate;
}
int main() {
	hako inp;
	hako hyouka;
	int a, b;
	int n, count;
	int resulttate[200] = {};
	int resultyoko[200] = {};
	hako tmp[30000];
	int nanko;
	for (count = 0; true; count++) {
		
		cin >> a >> b;
		inp.tate = a;
		inp.yoko = b;
		nanko = 0;
		if (a == 0 && b == 0) break;
		for (int i = 2; i < 150; i++) {
			for (int j = i-1; j > 0; j--) {
				hyouka.tate = j;
				hyouka.yoko = i;
				if (biggerthan(hyouka, inp)) {
					tmp[nanko].tate = j;
					tmp[nanko].yoko = i;
					nanko++;
				}
			}
		}
		sort(tmp, tmp + nanko, smallerthan);
			resulttate[count] = tmp[0].tate;
			resultyoko[count] = tmp[0].yoko;
	}
	for (int i = 0; i < count; i++) {
		cout << resulttate[i] << " " << resultyoko[i] << endl;
	}
	return 0;
}
	/*
	int n;
	long long int sumer = 0;
	int temp;
	int ave;
	int data[60000];
	int result[60000] = {};
	int count;
	for (count = 0; true; count++) {
		cin >> n;
		sumer = 0;
		temp = 0;
		if (n == 0) break;
		for (int i = 0; i < n; i++) {
			cin >> temp;
			data[i] = temp;
			sumer += temp;
		}
		ave = sumer / n;
		for (int i = 0; i < n; i++) {
			if (data[i] <= ave) {
				result[count]++;
			}
		}
		
	}
	for (int i = 0; i < count; i++) {
		cout << result[i] << endl;
	}
	return 0;
}
/*
//単一連結リストの実装
struct ListNode {
	int data;
	struct ListNode* next;
};*//*
int ListLength(struct ListNode* head) {
	struct ListNode* current = head;
	int count = 0;
	while (current != NULL) {
		count++;
		current = current -> next;
	}
	return count;
}


int main()
{
	int n = 0;
	int owari = 11451414;
	int result[50010] = {};
	int sa = owari;
	int count = 0;
	while (true) {
		int dataset[50010];
		cin >> n;
		if (n == 0) break;
		for (int i = 0; i < n; i++) {
			cin >> dataset[i];
		}
		sort(dataset, dataset + n);
		sa = owari;
		for (int i = 1; i < n; i++) {
			if (abs(dataset[i] - dataset[i - 1]) < sa) {
				sa = abs(dataset[i] - dataset[i - 1]);
			}
		}
		result[count] = sa;
		count++;
	}
	for (int i = 0; i < count; i++) {
		cout << result[i] << endl;
	}
	return 0;
}
*/