SRM 668 DIV 2 IsItASquare 600-point
2015-09-23 10:03
447 查看
Problem Statement
It’s a bird! It’s a plane! No, it’s a square in a plane! Wait, is it really a square? There are four distinct points in the plane. You are given their coordinates in the vector s x and y: for each i between 0 and 3, inclusive, there is a point at (x[i], y[i]). Return “It’s a square” (quotes for clarity) if the four points are the vertices of a square. Otherwise, return “Not a square”.Definition
Class:
IsItASquare
Method:
isSquare
Parameters:
vector , vector
Returns:
string
Method signature:
string isSquare(vector x, vector y)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Constraints
x will contain 4 elements.
y will contain 4 elements.
Each element of x will be between 0 and 10,000, inclusive.
Each element of y will be between 0 and 10,000, inclusive.
The four points described by x and y will be distinct.Examples
0)
{0, 0, 2, 2}
{0, 2, 0, 2}
Returns: “It’s a square”
1)
{0, 1, 5, 6}
{1, 6, 0, 5}
Returns: “It’s a square”
Note that the sides of the square do not have to be parallel to the coordinate axes. Also note that the order in which the points are given does not have to be the same as the order in which you would encounter them when following the boundary of the square.
2)
{0, 0, 7, 7}
{0, 3, 0, 3}
Returns: “Not a square”
3)
{0, 5000, 5000, 10000}
{5000, 0, 10000, 5000}
Returns: “It’s a square”
4)
{1, 2, 3, 4}
{4, 3, 2, 1}
Returns: “Not a square”
5)
{0, 5, 3, 8}
{0, 0, 4, 4}
Returns: “Not a square”
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
My Solution
#include <iostream> #include <vector> #include <string> using namespace std; class IsItASquare { public: string isSquare(vector <int> x, vector <int> y); private: //距离平方 double distance(int x1, int y1, int x2, int y2); //对角线的中点是否重合 bool isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); }; string IsItASquare::isSquare(vector <int> x, vector <int> y) { string ans; if (x.size() != 4 || y.size() != 4) { ans = "Not a square"; return ans; } for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 4; j++) if (x[i] == x[j] && y[i] == y[j]) { ans = "Not a square"; return ans; } } if (distance(x[0], y[0], x[1], y[1]) == distance(x[2], y[2], x[3], y[3]) && isMidSame(x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3]) && distance(x[0], y[0], x[2], y[2]) == distance(x[2], y[2], x[1], y[1]) ) ans = "It's a square"; if (distance(x[0], y[0], x[2], y[2]) == distance(x[1], y[1], x[3], y[3]) && isMidSame(x[0], y[0], x[2], y[2], x[1], y[1], x[3], y[3]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[2], y[2]) ) ans = "It's a square"; if (distance(x[0], y[0], x[3], y[3]) == distance(x[1], y[1], x[2], y[2]) && isMidSame(x[0], y[0], x[3], y[3], x[1], y[1], x[2], y[2]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[3], y[3]) ) ans = "It's a square"; ans = "Not a square"; return ans; } double IsItASquare::distance(int x1, int y1, int x2, int y2) { return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); } bool IsItASquare::isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) { if (x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4) return true; else return false; }
相关文章推荐
- 概率最大骰子总和 Topcoder SRM 536 DIV1 第2题
- TopCoder SRM 569 DIV2 Level3: MegaFactorialDiv2
- Topcoder SRM Div2 Level2
- Topcoder SRM 547 DIV1 Level 1: Pillars
- TopCoder SRM DIV2 Level 3: RelativelyPrimeSubset
- TopCoder SRM 558 DIV2 Level 3:CatAndRabbit
- Topcoder SRM642 TaroCutting
- topcoder arena 插件配置
- Convert from char to int OR Convert from int to char
- Topcoder Open 2014 Algorithm Round 1A
- TopCoder SRM 593: MayTheBestPetWin 势均力敌的赛跑
- TopCoder SRM 610: The Matrix 区分现实与梦境的棋盘
- Topcoder/SRM565
- TOPCODER/SRM565 DIVII 250、500pt(500pt无递归解法)
- TOPCODER/SRM 566 DIVII(250、500、1000题)(1000PT暂未附上代码)
- TOPCODE/SRM567 DIVII 250、500PT
- ACM-计算几何 #Topcoder #SRM187-DIV2 PointInPolygon
- TopCoder SRM 144 DIV2(200-point)
- TopCoder SRM 144 DIV2(550-point)
- ZigZag