SRM 668 DIV 2 IsItASquare 600-point

来源:互联网 发布:智能电视专用软件 编辑:程序博客网 时间:2024/05/18 03:22

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;}
0 0
原创粉丝点击