题目来源
题干
Find the minimum area of a square land on which you can place two identical rectangular a×b houses. The sides of the houses should be parallel to the sides of the desired square land.
Formally,
- You are given two identical rectangles with side lengths aa and bb (1≤a,b≤100) — positive integers (you are given just the sizes, but not their positions).
- Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square.
Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding.
The picture shows a square that contains red and green rectangles.
Input
The first line contains an integer t (1≤t≤10000) —the number of test cases in the input. Then t test cases follow.
Each test case is a line containing two integers aa, bb (1≤a,b≤100) — side lengths of the rectangles.
Output
Print t answers to the test cases. Each answer must be a single integer — minimal area of square land, that contains two rectangles with dimensions a×b.
Example
input
8
3 2
4 2
1 1
3 1
4 7
1 3
7 4
100 100
output
16
16
4
9
64
9
64
40000
Note
Below are the answers for the first two test cases:
题目大意
给你一个a×b的矩形,现在需要你找到一个能放下两个这样矩形的最小的正方形,求他的面积。
解题思路
显然将两个矩形以长边重合的形式摆放时,正方形面积最小。
实现
#include <iostream>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
int minSide = min(a, b) * 2; // minSide为二倍短边
if (minSide < max(a, b)) { // 将二倍短边与长边比较,谁大谁是正方形的边长
minSide = max(a, b);
}
cout << minSide * minSide << endl;
}
return 0;
}