UVa 11455 – Behold my quadrangle
Every square is a rectangle, every rectangle is a quadrilateral, and every quadrilateral has four sides. However, not all rectangles are squares, not all quadrilaterals are rectangles, and not every four sides can form a quadrilateral.
Problem: Given the lengths of four sides, you must determine if they can form a square. If not, determine if they can form a rectangle. If not, determine if they can form a quadrilateral.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input contains an integer representing the number of test cases. Each test case consists of a single line containing four positive integers ranging from 0 to 230. | For each test case, if the given lengths can form a square, rectangle, quadrangle, or none of them, respectively, output 'square', 'rectangle', 'quadrangle', or 'banana' on a single line. |
4 10 8 7 6 9 1 9 1 29 29 29 29 5 12 30 7 | quadrangle rectangle square banana |
Thought Process
Store the four side lengths into a vector and sort them.
Square: All four sides are equal in length.
Rectangle: The first two sides are equal in length, and the last two sides are equal in length.
Quadrilateral: The sum of the first three sides is greater than the fourth side.
Non-quadrilateral: Otherwise.
Sample Code-ZeroJudge D260: Behold my quadrangle
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i<N; i++)
{
vector<int>num;
bool zero = false;
for (int j = 0; j<4; j++)
{
int tmp;
cin >> tmp;
num.push_back(tmp);
if (tmp == 0) zero = true;
}
if (zero)
{
cout << "banana\n";
continue;
}
sort(num.begin(), num.end());
if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) cout << "square\n";
else if (num[0] == num[1] && num[2] == num[3]) cout << "rectangle\n";
else if (num[0] + num[1] + num[2] > num[3]) cout << "quadrangle\n";
else cout << "banana\n";
}
}
//ZeroJudge D260
//Dr. SeanXD