Minimal Square CodeForces - 1360A(简单思维和图形判断)
題意:
給你兩個大小一樣的,邊長為a,b的矩形將其放入一個正方形里,問怎樣放可以使正方形面積最小(要求正方形邊和矩形邊平行)
題目:
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 a and b (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 a, b (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:
分析:
確定正方形的邊長,可知當兩個矩形貼在一起,沒有多余空隙的時候面積最小,此時只要判斷下邊長情況。以較短的邊作為側邊y,那么較長的邊就為x;所求的正方形的邊則為a或者2*b,即為其中較大的那一個,就可以得到最小正方形面積。
AC代碼:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int t,a,b,ans; int main() {scanf("%d",&t);while(t--){scanf("%d%d",&a,&b);int x=a*2;int y=b*2;if(x>y){if(a>y)printf("%d\n",a*a);else printf("%d\n",y*y);}else{if(b>x)printf("%d\n",b*b);else printf("%d\n",x*x);}}return 0; }總結
以上是生活随笔為你收集整理的Minimal Square CodeForces - 1360A(简单思维和图形判断)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iQOO 12系列全量推送新版本 蓝心小
- 下一篇: Honest Coach CodeFor